Java : How to create a 64 bit array in java to store larger than Integer_max-value -
i trying find sum of primes. execute in smaller time have precomputed in long array last 2 test cases appearing wrong. reason overflow i.e. max value can accommodated in integer.max_value trying provide large value. need large value :
test link : https://www.hackerrank.com/contests/projecteuler/challenges/euler010/
this code :
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class solution { public static void main(string[] args) { /* enter code here. read input stdin. print output stdout. class should named solution. */ scanner sc = new scanner(system.in); int count,n,t; long ans[] = new long[10000001]; ans[0] = 0; ans[1] = 1; ans[2] = 2; ans[3] = 5; ans[4] = 5; // ans[2] = 5; for(int m=5;m<100000;){ count = 0; if(m%2==0){ count++; } // for(int i=3;i<=1000000;i++){ for(int j=3;j*j<=m;j+=2){ if(m%j==0){ // system.out.println(i); count++; } } if(count<1){ ans[m]=ans[m-1]+m; // system.out.println(m+" : "+ans[m]); m++; } else{ ans[m]=ans[m-1]; // system.out.println(m+" : "+ans[m]); m++; } // } } t = sc.nextint(); while(t>0){ n = sc.nextint(); system.out.println(ans[n]); t--; } } }
it looks trying store large number. can use java's biginteger class:
according java docs:
all operations behave if bigintegers represented in two's-complement notation (like java's primitive integer types). biginteger provides analogues of java's primitive integer operators, , relevant methods java.lang.math. additionally, biginteger provides operations modular arithmetic, gcd calculation, primality testing, prime generation, bit manipulation, , few other miscellaneous operations.
when doing system security, need generate large prime numbers more 100 digits long. use biginteger.
Comments
Post a Comment