We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Can someone tell why this code isn't working for range<10^18??
I know the max range in java for long is 10^19, but at least it should give correct for those test cases.!
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticintperfect(longn){longsum=n+1;for(longi=2;i<=Math.sqrt(n);i++){if(n%i==0)sum+=i;}if(sum==n)return1;elsereturn0;}publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannersc=newScanner(System.in);longn=sc.nextLong();longsum=0;for(longi=1;i<=n;i++){if(perfect(i)==1){longsigma=2*n;doublep=sigma/n;longk=(long)p;if(k-p==0.5)sum+=i;}}System.out.println(sum);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #241: Perfection Quotients
You are viewing a single comment's thread. Return to all comments →
Can someone tell why this code isn't working for range<10^18?? I know the max range in java for long is 10^19, but at least it should give correct for those test cases.!