You are viewing a single comment's thread. Return to all comments →
i tried your tip but am getting an error please help!
import java.io.*; import java.util.Arrays; class hackerrank { public static void main(String args[])throws IOException { System.out.println(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); for(int j=1;j<=t;j++) { int n=Integer.parseInt(br.readLine()); if(n%2==0) { Boolean a[]=new Boolean[n]; Arrays.fill(a,true); for(int i=2;i<Math.sqrt(n);i++) { if(a[i]==true) { int counter=0; for(int k=i*i;k<n;k=i*i+counter*i) { a[k]=false; counter++; } } } int ans=1; for(int i=2;i<n;i++) { int factor=1; if(a[i]==true) { int k=1; while(Math.pow(i,k)<=n) { factor=(int)Math.pow(i,k); k++; } } ans=ans*factor; } System.out.println(ans); } else { Boolean a[]=new Boolean[n+1]; Arrays.fill(a,true); for(int i=2;i<Math.sqrt(n);i++) { if(a[i]==true) { int counter=0; for(int k=i*i;k<n;k=i*i+counter*i) { a[k]=false; counter++; } } } int ans=1; for(int i=2;i<=n;i++) { int factor=1; if(a[i]==true) { int k=1; while(Math.pow(i,k)<=n) { factor=(int)Math.pow(i,k); k++; } } ans=ans*factor; } System.out.println(ans); } } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #5: Smallest multiple
You are viewing a single comment's thread. Return to all comments →
i tried your tip but am getting an error please help!