Memory error in java - java
I am trying to code in java prime number algorithm. I am trying to face the problems with the integer and long size. My simply code is the following which works until a limit for n:
public static long f(int n) {
if (n == 1 || n == 2)
return 1;
else {
long value = f(n-2) + f(n-1);
return value;
}
}
If in my main I ll give 50 for example my code will be crushed, I am guessing due to the size of the outcome. I ve got also another approach which I am struggle to understand it, which is the following:
private static long[] cache = new long[60];
public static long f(int n) {
if (n == 1 || n == 2)
return 1;
else if (cache[n] > 0)
return cache[n];
else {
long value = f(n-2) + f(n-1);
cache[n] = value;
return value;
}
}
With this approach everything works fine whatever is the n, my issue is that I cannot get the difference.
By "crushed" you mean that the computation runs very long. The reason is that the same call is made many times. If you add this to your method:
static long count;
public static long f(int n) {
count++;
...
you'll see how many times the method is executed. For f(50), it is actually calling the method 25,172,538,049 times, which runs in 41 seconds in my machine.
When you cache the result of previous invocations, called memoization, you eliminate all the redundant calls, e.g. f(40) = f(39) + f(38), but f(39) = f(38) + f(37), so f(38) is called twice. Remembering the result of f(38) means that subsequent invocation has the answer immediately, without having to redo the recursion.
Without memoization, I get this:
n f(n) count time(ns)
== ============== =============== ==============
1 1 1 6,273
2 1 1 571
3 2 3 855
4 3 5 1,141
5 5 9 1,425
6 8 15 1,140
7 13 25 1,996
8 21 41 2,851
9 34 67 7,413
10 55 109 16,536
11 89 177 8,839
12 144 287 19,103
13 233 465 21,098
14 377 753 11,405
15 610 1,219 5,703
16 987 1,973 9,979
17 1,597 3,193 21,099
18 2,584 5,167 32,788
19 4,181 8,361 35,639
20 6,765 13,529 57,307
21 10,946 21,891 91,521
22 17,711 35,421 147,687
23 28,657 57,313 237,496
24 46,368 92,735 283,970
25 75,025 150,049 331,583
26 121,393 242,785 401,720
27 196,418 392,835 650,052
28 317,811 635,621 1,053,483
29 514,229 1,028,457 1,702,679
30 832,040 1,664,079 2,750,745
31 1,346,269 2,692,537 4,455,137
32 2,178,309 4,356,617 12,706,520
33 3,524,578 7,049,155 11,714,051
34 5,702,887 11,405,773 19,571,980
35 9,227,465 18,454,929 30,605,757
36 14,930,352 29,860,703 51,298,507
37 24,157,817 48,315,633 84,473,965
38 39,088,169 78,176,337 127,818,746
39 63,245,986 126,491,971 208,727,118
40 102,334,155 204,668,309 336,785,071
41 165,580,141 331,160,281 543,006,638
42 267,914,296 535,828,591 875,782,771
43 433,494,437 866,988,873 1,429,555,753
44 701,408,733 1,402,817,465 2,301,577,345
45 1,134,903,170 2,269,806,339 3,724,691,882
46 1,836,311,903 3,672,623,805 6,010,675,962
47 2,971,215,073 5,942,430,145 9,706,561,705
48 4,807,526,976 9,615,053,951 15,715,064,841
49 7,778,742,049 15,557,484,097 25,427,015,418
50 12,586,269,025 25,172,538,049 41,126,559,697
If you get a StackOverflowError, it is due to the recursive nature of your algorithm. The second algorithm stores known results into an array to prevent functions piling up when it asks for an already computed result.
One of the problem can be the big number result, which is more than integer limit:
fibonacci for 50 =
12,586,269,025
2,147,483,647 - int max
and the other can be due to the recursion stackoverflow like #Xalamadrax pointed.
Related
Array Shuffle (With same Start and end Value)
I'm currently working on a Travelling salesman problem and i'm having trouble generating random paths with the same start and end value This is my current path(path of cities visited) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 38 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 14 39 40 41 42 43 44 45 46 47 48 49 50 51 0 See how the last node arrives back at 0(TSP logic) Now i'm trying to shuffle this array randomly because random start points generate better results then others However I know how to shuffle the array using Collections but that would shuffle every value randomly Basically i'm trying to create a method to shuffle an array randomly WITH THE EXCEPTION THE START and END VALUES must be the same and every number must be distinct 0-51 This is my current code, it basically shuffles the array and sets the last index to the first index static void shuffleArray(int[] ar) { // If running on Java 6 or older, use `new Random()` on RHS here Random rnd = new Random(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } ar[ar.length-1]=ar[0]; } however this is giving me duplicate values for some reason
Could you not remove the start and end position, shuffle the array, and then add the start and end position again?
How to set *any* bit in a Java long, even if bit position > 30
I'm familiar with how to set and clear specific bits in a Java numeric variable. But I'm not sure how to manipulate any bits in position > 30 in a long. I've read that Java handles longs as two ints for heap sizes less than 30gb. Setting my heap size to 45gb (on an EC2 instance) didn't help. I confirmed that I'm using 64 bit Java: $ java -version openjdk version "1.8.0_71" OpenJDK Runtime Environment (build 1.8.0_71-b15) OpenJDK 64-Bit Server VM (build 25.71-b15, mixed mode) Is there any way to change bits in any position in a Java long? I think this is the same question, but for C. But it wasn't helpful for me. Here is the code and output that illustrates my predicament. As you can see, it works great up through the 31st bit. private static long setBit(long bits, int bitPosition) { return bits | (1 << bitPosition); } private static long clearBit(long bits, int bitPosition) { return bits & ~(1 << bitPosition); } public static void main(String[] args) { for (int i = 0; i < 64; i++) { long myLong = clearBit(Long.MAX_VALUE, i); System.out.println(Long.toBinaryString(myLong) + " " + i + " " + myLong); } } output: 111111111111111111111111111111111111111111111111111111111111110 0 9223372036854775806 111111111111111111111111111111111111111111111111111111111111101 1 9223372036854775805 111111111111111111111111111111111111111111111111111111111111011 2 9223372036854775803 111111111111111111111111111111111111111111111111111111111110111 3 9223372036854775799 111111111111111111111111111111111111111111111111111111111101111 4 9223372036854775791 111111111111111111111111111111111111111111111111111111111011111 5 9223372036854775775 111111111111111111111111111111111111111111111111111111110111111 6 9223372036854775743 111111111111111111111111111111111111111111111111111111101111111 7 9223372036854775679 111111111111111111111111111111111111111111111111111111011111111 8 9223372036854775551 111111111111111111111111111111111111111111111111111110111111111 9 9223372036854775295 111111111111111111111111111111111111111111111111111101111111111 10 9223372036854774783 111111111111111111111111111111111111111111111111111011111111111 11 9223372036854773759 111111111111111111111111111111111111111111111111110111111111111 12 9223372036854771711 111111111111111111111111111111111111111111111111101111111111111 13 9223372036854767615 111111111111111111111111111111111111111111111111011111111111111 14 9223372036854759423 111111111111111111111111111111111111111111111110111111111111111 15 9223372036854743039 111111111111111111111111111111111111111111111101111111111111111 16 9223372036854710271 111111111111111111111111111111111111111111111011111111111111111 17 9223372036854644735 111111111111111111111111111111111111111111110111111111111111111 18 9223372036854513663 111111111111111111111111111111111111111111101111111111111111111 19 9223372036854251519 111111111111111111111111111111111111111111011111111111111111111 20 9223372036853727231 111111111111111111111111111111111111111110111111111111111111111 21 9223372036852678655 111111111111111111111111111111111111111101111111111111111111111 22 9223372036850581503 111111111111111111111111111111111111111011111111111111111111111 23 9223372036846387199 111111111111111111111111111111111111110111111111111111111111111 24 9223372036837998591 111111111111111111111111111111111111101111111111111111111111111 25 9223372036821221375 111111111111111111111111111111111111011111111111111111111111111 26 9223372036787666943 111111111111111111111111111111111110111111111111111111111111111 27 9223372036720558079 111111111111111111111111111111111101111111111111111111111111111 28 9223372036586340351 111111111111111111111111111111111011111111111111111111111111111 29 9223372036317904895 111111111111111111111111111111110111111111111111111111111111111 30 9223372035781033983 1111111111111111111111111111111 31 2147483647 111111111111111111111111111111111111111111111111111111111111110 32 9223372036854775806 111111111111111111111111111111111111111111111111111111111111101 33 9223372036854775805 111111111111111111111111111111111111111111111111111111111111011 34 9223372036854775803 111111111111111111111111111111111111111111111111111111111110111 35 9223372036854775799 111111111111111111111111111111111111111111111111111111111101111 36 9223372036854775791 111111111111111111111111111111111111111111111111111111111011111 37 9223372036854775775 111111111111111111111111111111111111111111111111111111110111111 38 9223372036854775743 111111111111111111111111111111111111111111111111111111101111111 39 9223372036854775679 111111111111111111111111111111111111111111111111111111011111111 40 9223372036854775551 111111111111111111111111111111111111111111111111111110111111111 41 9223372036854775295 111111111111111111111111111111111111111111111111111101111111111 42 9223372036854774783 111111111111111111111111111111111111111111111111111011111111111 43 9223372036854773759 111111111111111111111111111111111111111111111111110111111111111 44 9223372036854771711 111111111111111111111111111111111111111111111111101111111111111 45 9223372036854767615 111111111111111111111111111111111111111111111111011111111111111 46 9223372036854759423 111111111111111111111111111111111111111111111110111111111111111 47 9223372036854743039 111111111111111111111111111111111111111111111101111111111111111 48 9223372036854710271 111111111111111111111111111111111111111111111011111111111111111 49 9223372036854644735 111111111111111111111111111111111111111111110111111111111111111 50 9223372036854513663 111111111111111111111111111111111111111111101111111111111111111 51 9223372036854251519 111111111111111111111111111111111111111111011111111111111111111 52 9223372036853727231 111111111111111111111111111111111111111110111111111111111111111 53 9223372036852678655 111111111111111111111111111111111111111101111111111111111111111 54 9223372036850581503 111111111111111111111111111111111111111011111111111111111111111 55 9223372036846387199 111111111111111111111111111111111111110111111111111111111111111 56 9223372036837998591 111111111111111111111111111111111111101111111111111111111111111 57 9223372036821221375 111111111111111111111111111111111111011111111111111111111111111 58 9223372036787666943 111111111111111111111111111111111110111111111111111111111111111 59 9223372036720558079 111111111111111111111111111111111101111111111111111111111111111 60 9223372036586340351 111111111111111111111111111111111011111111111111111111111111111 61 9223372036317904895 111111111111111111111111111111110111111111111111111111111111111 62 9223372035781033983 1111111111111111111111111111111 63 2147483647 Thank you for reading - any guidance/code/suggestions would be greatly appreciated!
1<<32 will overflow since it is int. You have to change it to long. return bits | (1L << bitPosition)
private static long clearBit(long bits, int bitPosition) { return bits & ~(1l << bitPosition); } 1 without a l is a int, that is your bug.
ParseInt keeps getting a zero
I am reading a text file in from another program that is this(I really just am trying to save the first number of each line. I am simulating a memory and CPU interaction so these are codes in another class: 1 // Load 0 0 14 // CopyToX 4 // LoadIdxX 32 (load from A-Z table) 32 21 // JumpIfEqual 12 12 9 // Put 2 (output as char) 2 25 // IncX 20 // Jump 3 3 1 // Load 0 0 16 // CopyToY 5 // LoadIdxY 59 (load from 1-10 table) 59 21 // JumpIfEqual 27 27 9 // Put 1 (output as int) 1 1 // Load 1 (because no IncY instruction) 1 11 // AddY 16 // CopyToY 20 // Jump 15 15 1 // Print newline 10 9 2 50 // End 65 // Data A-Z 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 0 1 // Data 1-10 2 3 4 5 6 7 8 9 10 0 .1000 30 I am trying to just save it into the memory array, but while debugging it I see that the value stored is zero. I cant figure it out! I added the "[0]" because the compiler would throw this error without it The method parseInt(String) in the type Integer is not applicable for the arguments (String[]) So, any help is much appreciated here is the block of code! String line; int init=0; while((line= Fileread.readLine())!=null) { System.out.println(line.length()); if(line.length()==0) break; try { memory[init++]=Integer.parseInt(line.trim().split(" ")[0]); System.out.println(memory[init]); } catch(NumberFormatException e) { init=Integer.parseInt(line.substring(1)); } }
You're incrementing init before you actually print the value. So your code is printing 0 because you haven't assigned anything to memory[init] yet. String line; int init=0; while((line= Fileread.readLine())!=null) { System.out.println(line.length()); if(line.length()==0) break; try { memory[init]=Integer.parseInt(line.trim().split(" ")[0]); System.out.println(memory[init]); init++; } catch(NumberFormatException e) { init=Integer.parseInt(line.substring(1)); } }
Your code could be something like this: String line; int init=0; while((line= Fileread.readLine())!=null) { System.out.println(line.length()); if(line.length()==0) break; String num= str.replaceAll("\\D+",""); try { if(num.length() == 0){ memory[init++]=Integer.parseInt(num); System.out.println(memory[init]); } } catch(NumberFormatException e) { init=Integer.parseInt(line.substring(1)); } }
How to define a starting point in the condition?
The problem: I want all numbers from 1-50 that are not divisible by 7, but also don't have a 7 in them, like 17,27,etc. The code below works but (i-10)%7 has to start from i=6. Now it thinks the number 3 doesn't count cause (3-10)=-7 that is 0 mod 7. how do i solve this in the if statement? for(int i=1; i<=50;i++){ if(i%7!=0 && (i-10)%7!=0){ System.out.println(i);
Your second condition is wrong. 27 ends in 7, but (27-10)%7!=0. You just need to check that i % 10 is not 7. if(i%7!=0 && i%10!=7)
This will do the trick for(int i=1; i<=50;i++){ if(i==7 || i==3) { if(i==3) System.out.println(i); } else if(i%7!=0 && (i%10)%7!=0){ System.out.println(i);} } output is... 1 2 3 4 5 6 8 9 11 12 13 15 16 18 19 22 23 24 25 26 29 31 32 33 34 36 38 39 41 43 44 45 46 48 UPDATE If you want your if condition to start checking from i=6 then you can do this... for(int i=1; i<=50;i++){ if(i>=6 && i%7!=0 && (i%10)%7!=0) System.out.println(i); } but the output will change to... 6 8 9 11 12 13 15 16 18 19 22 23 24 25 26 29 31 32 33 34 36 38 39 41 43 44 45 46 48
This should work. Casting it to a string and just checking if it contains the string "7". if(i % 7 != 0 && ((String)i).contains("7"))
Its seems to basic: find if the number has 7 or not : number%10==7 find if the number is divisible by 7 or not : number%7==0 combine both in Or clause if (number%10==7||number%7==0) do watever....... but it only applicable if the number<50
If you're checking if each number contains a 7 instead of '(i-10)%7!=0' I would recommend casting i to a string (i + "") and then checking if it contains the char 7 using only i%10 !=7 will only check to see if the last digit is not 7.
Got wrong output in my task, cant solve it
Hello i got one task to do. output should be like this: { 0 0 1 0 2 4 0 3 6 9 0 4 8 12 16 0 5 10 15 20 25 0 6 12 18 24 30 36 0 7 14 21 28 35 42 49 0 8 16 24 32 40 48 56 64 0 9 18 27 36 45 54 63 72 81 } I tried to do it: here is my code: public class ContnueUzOznakoMojNacin { public static void main(String args[]) { int k=0, v=0; int j; for(int i=0;i<10;i++) { for(j=10-i;j<10;j++) { System.out.print(v+" "); v+=k; } System.out.println(); v=0; k++; } } } And the output i get is wrong and i dont get it why. here it is: { 0 0 2 0 3 6 0 4 8 12 0 5 10 15 20 0 6 12 18 24 30 0 7 14 21 28 35 42 0 8 16 24 32 40 48 56 0 9 18 27 36 45 54 63 72 } When i follow these loops and increments from my program i cant find mistake. first line is ok output should be 0; but second line, output should be 0 1; not 0 2 ? I dont need you to give me code for this task i need you to help me to do it, to tell me where i made mistake so i do it on my own. Thanks :)
Change this line: for(j=10-i;j<10;j++) to this: for(j=9-i;j<10;j++) And here's an explanation: So i starts at 0, right? What will be i's maximum? 9, because in for(int i=0;i<10;i++), i cannot get to 10. Let's look at how that affects for(j=10-i;j<10;j++). If i is 9 (the last row), then the j loop will only run 9 times. j will equal 1,2,3,4,5,6,7,8,9. That's only 9 loops. If you look at the bottom of the upper triangle, you can see that 0 9 18 27 36 45 54 63 72 has only 9 numbers. We want j to run 10 times, as you can see by the base of the correct triangle: 0 9 18 27 36 45 54 63 72 81. How do we do this? We make j run one more time on every i loop, by decreasing the starting number (10-i) by one (which equals 9-i). That is how you arrive at for(j=9-i;j<10;j++)