Project Euler 14 - Java StackOverflowError - java

For those unfamiliar with the problem, here it is.
I am getting a StackOverflowError with the following code:
public class LongestCollatzSequence {
static int count = 1;
public static void main(String[] args) {
final int range = 1000000;
int maxSeq = 0;
List<Integer> chainList = new ArrayList<Integer>();
for(int i = 1; i <= range; i++) {
generateSequence(i);
if(chainList.isEmpty()) {
chainList.add(count);
count = 1;
} else if(!chainList.contains(count) && count > Collections.max(chainList)) {
chainList.clear();
chainList.add(count);
maxSeq = i;
count = 1;
}
}
System.out.println("Sequence starting number: "+maxSeq);
}
private static void generateSequence(int num) {
if(num == 1) {
return;
}
if(num % 2 == 0) {
count++;
generateSequence(num/2);
} else {
count++;
generateSequence(num*3+1);
}
}
}
High level flow:
-For numbers 2 - 100000, generate a collatz sequence for that number.
-Chain list is a list to store the length of the sequence generated for each number i.e. sequence size for number 13 is 10 (see example).
-If the current sequence size is bigger than the max in the chain list, clear chain list and add the new max, also store the value of i in maxReq to remember the starting number that produces the longest chain.

Interesting problem but the int in java is limited to 2^31-1 you could crox this limit use long or BigInteger
private static void generateSequence(long num) {
if (num == 1) {
return;
}
if (num % 2 == 0) {
count++;
generateSequence(num / 2);
} else {
count++;
generateSequence(num * 3 + 1);
}
}

Related

Checking if an integer is a circular prime number without using String

I have to write a boolean method that checks if a number n is a circular prime, using only integer computations, so no Strings. I wrote two other methods that have to be included.
boolean isPrime(int n) {
if (n < 1) {
return false;
} else if (n == 1 || n == 2) {
return true;
} else if (n % 2 != 0) {
for (int i = 3; i < n; i+=2) {
if (n % i == 0) {
return false;
}
}
return true;
} else {
return false;
}
}
This checks if the number is a prime.
int largestPowerOfTen(int n) {
for (int i = 1; i < n * 10; i*=10) {
if (n / i == 0) {
return i / 10;
}
}
return 1;
}
This returns the largest power of ten of the number. For instance, 23 would return 10, 704 would return 100, etc.
I had the idea to put every digit into an array and move the digits around from there, but I'm stuck at the moving part.
boolean isCircularPrime(int n) {
ArrayList<Integer> k = new ArrayList<Integer>();
int i = 0;
while (n != 0) {
k.add(n % 10);
n /= 10;
i++;
}
//???
}
So how do I move the digits around?
Assuming a "circular prime number" is a number that is a prime number for all rotations of the digits...
You can't just rotate the number, because zeroes won't be conserved.
First break up the number into an array - each digit of the number an element of the array. Use n % 10 to find the last digit, then n /= 10 until n == 0.
Create a method the generates a number from the array with a specified starting index. This is the crux of the problem, and here's some code:
private static int generate(int[] digits, int index) {
int result = 0;
for (int i = 0; i < digits.length; i++) {
result = result * 10 + digits[(index + i) % digits.length];
}
return result;
}
Then loop over every possible starting index for your digits and check if it's prime.
The remaining code I leave to the reader...
import java.util.Scanner;
class CircularPrime
{
public boolean prime(int n)
{
int lim=n,count=0;
for(int i=1;i<=lim;i++)
{
if(n%i==0)count++;
}
if(count==2)
return true;
else
return false;
}
public int circlize(int n)
{
int len,x,y,circle;
len=(""+n).length();
x=n/(int)Math.pow(10,len-1);
y=n%(int)Math.pow(10,len-1);
circle=(y*10)+x;
return circle;
/**
Another way using String
String str = Integer.toString(n);
String arr = str.substring(1)+str.charAt(0);
int a = Integer.parseInt(arr);
return a;
**/
}
public void check(int n)
{
int a=n;
boolean flag=true;
System.out.println("OUTPUT:");
do
{
if(!(prime(a)))
{
flag=false;
break;
}
a=circlize(a);
System.out.println(a);
}while(a!=n);
if(flag)System.out.println(n+" IS A CIRCULAR PRIME");
else System.out.println(n+" IS NOT A CIRCULAR PRIME");
}
public static void main(String ar[])
{
CircularPrime obj = new CircularPrime();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
obj.check(n);
}
}

Collatz Conjecture Recursion Error

I've lurked around for years, but today I have a question concerning my code. I am currently trying to create a collatz program that puts the number of steps for a certain number in an array, but at the same time puts the number of steps for every single number it passes through. Here's my code:
public class GenerousRecursion {
public static short steps;
public static int length;
public static short[] array = new short[101];
public static void main(String[] args) {
length = 100;
for (int count = 2; count < length + 1; count++){
steps = 0;
System.out.println(count + ": " + findCollatz(count));
}
}
public static short findCollatz(int number) {
if (number < length){
if (array[number] > 0) {
steps = array[number]++; return steps;
}
else if(number % 2 == 0) {
array[number] = findCollatz(number / 2);
steps ++;
return steps;
}
else {
array[number] = findCollatz(3 * number + 1);
steps ++;
return steps;
}
}
else {
if(number % 2 == 0) {
findCollatz(number / 2);
steps ++;
return steps;
}
else {
findCollatz(3 * number + 1);
steps ++;
return steps;
}
}
}
}
Here's a great video on the Collatz Conjecture: Numberphile
So here is the error being thrown (reduced), but I don't understand, because I am not anywhere near the bounds of any int or short:
Exception in thread "main" java.lang.StackOverflowError
at GenerousRecursion.findCollatz(GenerousRecursion.java:22)
at GenerousRecursion.findCollatz(GenerousRecursion.java:33)
at GenerousRecursion.findCollatz(GenerousRecursion.java:27)
I just listed these first three lines because these same three lines draw errors for hundreds of lines.
Whats the problem and how do I fix it? Thanks abunch!
Edit: When I ran the debugger, my program continously throws exceptions whenever the array is refrenced.
As stated in the video-clip continuing with 1 will end in an endless loop.
Try the following.
static int[] collatzCounts = new int[100];
static final int NO_COUNT = -1;
static {
Arrays.fill(collatzCounts, NO_COUNT);
collatzCounts{1] = 0; // Define collatz(1) = 0 (1, 4, 2, 1, ...)
}
public static void main(String[] args) {
for (int n = 2; n < 120; n++) {
int steps = countCollatz(n);
System.out.println(n + ": " + steps);
}
}
public static int countCollatz(int n) {
IntFunction f = k ->
k % 2 == 0
? 1 + countCollatz(k / 2)
: 1 + countCollatz(3 * k + 1);
//: 2 + countCollatz((3 * k + 1) / 2);
//if (n == 1) {
// return 0;
//}
if (n < collatzCounts.length) {
if (collatzCounts[n] == NO_COUNT) {
collatzCounts[n] = f.apply(n);
}
return collatzCounts[n];
} else {
return f.apply(n);
}
}
countCollatz simply counts the steps needed - to reach 1 actually. Though till further proof there might be a cycle of higher numbers.
I have used a Java 8 lambda expression, the IntFunction f, as it is more natural to repeat the calculation, once to fill the array, once for too large numberss.

calculating consecutive 1's in a Binary number

import java.util.Scanner;
import java.util.Arrays;
class Solve
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i=0,count=0;
int[] arr = new int[10];
int n =in.nextInt();
while(n!=0)
{
arr[i]=n%2;
i++;
n=n/2;
}
System.out.println(Arrays.toString(arr));
}
}
}
I just want to calculate number of consecutive 1's. ? like 1110011001 will give me answer 5.. How can i do that??
System.out.println(Integer.toBinaryString(n).replaceAll("(0|(?<!1)1(?!1))", "").length());
The regex means: replace all 0's and any 1 not preceded or followed by another 1
You can handle this as a String [Edited to sum all consecutive 1's]:
String binary = in.nextLine();
String[] arrayBin = binary.split("0+"); // an array of strings without 0's
int result=0;
for (int i=0; i < arrayBin.length; i++){
if (arrayBin[i].length()<2){
result+=0;
}
else {
result+=arrayBin[i].length();
}
}
System.out.println("Total consecutive = "+result);
We can identify two consecutive binary ones in the least significant positions like this:
(value & 0b11) == 0b11
We can move the bits in value to the right like so:
value >>>= 1;
It's important to use tripple >>> over double >> because we don't care about the sign bit.
Then all we have to do is keep track of the number of consecutive 1s:
int count(int value) {
int count = 1;
int total = 0;
while (value != 0) {
if ((value & 0b11) == 0b11) {
count++;
} else {
if (count > 1) {
total += count;
}
count = 1;
}
value >>>= 1;
}
return total;
}
Test cases:
assertEquals(0, count(0b0));
assertEquals(0, count(0b1));
assertEquals(0, count(0b10));
assertEquals(2, count(0b11));
assertEquals(5, count(0b1110011));
assertEquals(5, count(0b1100111));
assertEquals(6, count(0b1110111));
assertEquals(7, count(0b1111111));
assertEquals(32, count(-1));
If you only want the length of the maximum, I have a similar answer: https://stackoverflow.com/a/42609478/360211
You can make use of Brian Kernighan’s Algorithm for counting the highest consecutive number of 1's.
A java pseudocode would be something like this
// Initialize result
int count = 0;
// Count the number of iterations to
// reach n = 0.
while (n!=0)
{
// This operation reduces length
// of every sequence of 1s by one.
n = (n & (n << 1));
count++;
}
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int counter = 0, max = Integer.MIN_VALUE;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 1){
counter += nums[i];
} else{
counter = nums[i];
}
max = Math.max(counter, max);
}
return max;
}
}
To this problem one trick which we can use here with help of some Java operators.
& operator and left shift (<<) in java.
Code snippet will be like :
public getConsecutiveCount(int inputNumber)
{
int count = 0 ;
while(inputNumber != 0)
{
inputNumber = inputNumber & (inputNumber << 1);
count++;
}
}
Explanation :
This function is taking input (ex : we want to check how many
consecutive 1's integer 6 have in its binary representation)
so out input number will be like :
inputNumber = ((110) & ((110)<<1)) {This left shift will result in 100 so final op :
110 & 100 which 100 , every time '0' is added to
our result and we iterate until whole number will
be zero and value of our count variable will be
our expected outcome }
To find Maximum consecutive 1's in binary(like 101)
int n = Convert.ToInt32(Console.ReadLine());
string[] base2=Convert.ToString(n,2).Split('0');
int count=0;
foreach(string s in base2)
count=s.Length>count?s.Length:count;
Console.WriteLine(count);
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String bs = Integer.toBinaryString(n);// bs=Binary String
char[] characters = bs.toCharArray();
int max = 1;
int temp = 1;
for (int i = 0; i < characters.length - 1; i++) {
if (characters[i] == characters[i + 1] & characters[i] == '1' & characters[i + 1] == '1') {
temp++;
if (temp > max) {
max = temp;
}
} else {
temp = 1;
}
}
System.out.println(max);
}
/* Given a decimal number print maximum number of consecutive 1's after binary conversion */
import java.io.*;
import java.util.*;
public class Solution {
public void countBinaryOne(int num){
int var =0, countOne= 0, maxCt=0;
while(num>0){
var= num%2;
if(var==1){
countOne=countOne+1;
}else{
if(maxCt<countOne){
maxCt= countOne;
countOne=0;
}else{
countOne=0;
}
}
num=num/2;
}
System.out.println(Math.max(countOne,maxCt));
}
public static void main(String[] args) {
Scanner in= new Scanner(System.in);
int n= in.nextInt();
Solution sol= new Solution();
sol.countBinaryOne(n);
}
}
public static void digitBinaryCountIfOne(int n){
int reminder=0, sum=0, total = 0;
while(n>0)
{
reminder = n%2;
n/=2;
if(reminder==1){
sum++;
if(sum>=total)
total=sum;
}else{
sum=0;
}
}
System.out.println(total);
}

Java Largest Prime Factor of Long

I'm trying to find the largest prime factor of a large number. For example, if that number were 573849284703, my code would look like this:
public static void main(String[] args) {
long number = 573849284703l;
System.out.println(lgstprmfactor(number));
}
public static long lgstprmfactor(long number) {
for (long i = 286924642352l; i > 0; i--) {
if (number % i == 0 && isPrime(i) == true) {
long answer = i;
return answer;
}
}
return 0;
}
public static boolean isPrime(long i) {
for (long c = 2; c < i; c++) {
if (i % c == 0)
return false;
}
return true;
}
But it's taking forever to run- any suggestions to speed it up or optimize the code in general?
One quick solution to improve runtime could be to implement your algorithm in multiple threads that concurrently check if the number is a prime factor across different ranges. I.e. create a thread that checks if it is a prime factor between 0 and 1000000, then a thread for 1000001+ etc.
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
System.out.println(largestprimefactor(573849284703l));
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime+" ms ");
}
public static int largestprimefactor(long l)
{
int i;
long copyofinput = l;
for(i=2;i<copyofinput;i++)
{
if(copyofinput%i==0){
copyofinput/=i;
i--;
}
}
return i;
}
}
output :
66718903
688 ms
The basic ideas here: remove prime factors as you find them, don't search higher than the square root of the remaining number, and skip even numbers (other than 2). I also added some error checking and other decorations.
public static void main(String[] args)
{
try {
System.out.println(largestPrimeFactor(573849284703l));
} catch (ArithmeticException e) {
System.out.println("Error factoring number: " + e.getMessage());
}
}
private static long sqrtint(long n) {
return (long)Math.sqrt(n + 0.5);
}
public static int largestPrimeFactor(long n) throws ArithmeticException
{
if (n < 2) throw new ArithmeticException(n + " < 2");
while (n%2 == 0) n /= 2;
if (n < 2) return 2;
long i, root = sqrtint(n);
for(i=3; i<root; i+=2)
{
if(n%i == 0) {
n /= i;
while (n%i==0) n /= i;
if (n == 1) return i;
root = sqrtint(n);
}
}
return n;
}
}

Adding square of the digits in java

Recently i attended one interview there they asked me to write a program like below,
A number chain is created by continously adding the square of the digits in a number
to form a new number untill its has been seen before.
example :
44 -> 32 -> 13-> 10-> 1
85->89->145->42->20->4->16->37->58->89
therefore any chain arrives at 1 or 89 will become stuck in endless loop.
what is most amazing is that every starting number will eventually arrive at 1 or 89.
write a program to count the starting number below 10000 will arrive at 89?
I wrote programs like below,
int num =44;
int array[] = new int[100];
int power=0;
while(num > 0)
{
int mod = num % 10;
int div = num /10;
int sum =(mod * mod) + (div * div);
num =sum;
System.out.print(" => "+sum);
if(array.equals(sum))
// should not use any functions like Arrays.asList(array).contains(sum);
return;
else
{
//System.out.println("else");
array[power++] =sum;
}
}
I know that above program not satisfy their requirements.Some one tell me good code snip to make them code satisfaction(If same question ask in future).?
Note : should not use any function or import. Only logic is need.
Maintain a cache of already calculated numbers. This will reduce the number of unnecessary iterations which were already calculated.
Let's do some math
From you example
44 -> 32 -> 13-> 10-> 1
85->89->145->42->20->4->16->37->58->89
You already know that the numbers 85, 89, 145, 42, 20, 4, 16, 37, 58 lead to 89 and 44, 32,13, 10, 1 don't.
In some case say calculating it for 11.
11 - 2 - 4
Now we already know 4 leads to 89 and so we skip all the other unnecessary iteration from
4 - 16 - 37 - 58 - 89 and also we now know that 11, 2also lead to 89.
So no the algorithm would be:
while(num > 0)
{
if(calculate[i] == 1)//which mean leads to 89
{
// dont calculate again
}
else
{
// num = // your squares logic
}
}
I read this as a recursive problem. I made the assumption that you only want to know the number of steps until 1 or 89 is reached.
The helper method, getDigits() is just for simplicity. The conversion to the String isn't technically necessary, but it makes the code simple.
public static void main(String[] args) {
int v = 9843;
int[] count = {0};
System.out.println("Number of steps: " + countSteps(v,count)[0]);
}
private static int[] countSteps(int initialValue, int[] count){
if(initialValue == 1 || initialValue == 89){
return count;
}
count[0]++;
int[] digits = getDigits(initialValue);
initialValue = 0;
for (int k : digits) {
initialValue += k * k;
}
countSteps(initialValue,count);
return count;
}
private static int[] getDigits(int i){
String s = Integer.toString(i);
int[] digits = new int[s.length()];
for(int j=0;j<s.length();j++){
digits[j] = s.charAt(j) - '0';
}
return digits;
}
If you are generating a sequence it makes sense to use an Iterable.
public static class SquaredDigitsSequence implements Iterable<Integer> {
int start;
SquaredDigitsSequence(int start) {
this.start = start;
}
#Override
public Iterator<Integer> iterator() {
return new SquaredDigitsIterator(start);
}
static class SquaredDigitsIterator implements Iterator<Integer> {
int last;
Set<Integer> seen = new HashSet<>();
SquaredDigitsIterator(int start) {
last = start;
seen.add(last);
}
#Override
public boolean hasNext() {
return !seen.contains(step());
}
#Override
public Integer next() {
last = step();
seen.add(last);
return last;
}
int step() {
int next = 0;
for (int x = last; x != 0; x /= 10) {
next += (x % 10) * (x % 10);
}
return next;
}
}
}
public void test() {
for (int i = 0; i < 100; i++) {
System.out.print(i + " ");
for (int x : new SquaredDigitsSequence(i)) {
System.out.print(x + " ");
}
System.out.println();
}
}
This prints all sequences up to 100 and does indeed include the two examples you posted but sadly it does not always terminate at 1 or 89.
class SquareDigits{
public static void main(String[] args){
System.out.println("Amount of numbers ending on 89: " + loop(10000));
}
public static int loop(int limit){
int cnt = 0;
for(int i = 1; i < limit; i++){
if(arriveAt89(i))
cnt++;
}
return cnt;
}
public static boolean arriveAt89(int num){
while(num != 89 && num != 1){
num = addSquares(num);
}
if(num == 89)
return true;
return false;
}
public static int addSquares(int n){
int sum = 0;
for(Character c : ("" + n).toCharArray()){
sum += Character.getNumericValue(c)*Character.getNumericValue(c);
}
return sum;
}
}
Assuming you're just after the amount of numbers that ends with 89.
This prints all numbers that end up in 89 below 89.
//array = boolean array to hold cache
//arr= list to store numbers in the chain that goes up to 89 used in setting cache
public static void main(String args[]) {
Boolean[] array = new Boolean[100];
Arrays.fill(array, Boolean.FALSE);
for(int i=2;i<89;i++){
checkChain(i,array);
}
for(int k=0;k<89;k++){
if(array[k]){System.out.println(k);}
}
}
private static void checkChain(int num,Boolean[] array) {
List<Integer> arr= new ArrayList<Integer>();
int initial = num;
int next;
do{
next=0;
arr.add(num);
while(num>0){
if(array[num] || num==89){
for(Integer j:arr){
array[j]=true;
}
break;
}
next = next+(num%10)*(num%10);
num=num/10;
}
num=next;
if(next<initial && array[next]){
array[initial]=true;
break;
}
}while((next>initial));
}
}

Categories

Resources