Created
May 5, 2015 08:14
-
-
Save stephen-maina/8301b6a28e4861d38ec2 to your computer and use it in GitHub Desktop.
not yet fast enough , cut speed by half
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Set; | |
| import java.util.HashSet; | |
| class Solution { | |
| public int[] solution(int N, int[] P, int[] Q) { | |
| // write your code in Java SE 8 | |
| int result[]=new int [Q.length]; | |
| for(int index=0;index<P.length;index++){ | |
| result[index]=sieve(P[index],Q[index]);; | |
| } | |
| return result; | |
| } | |
| public int sieve(int start,int N){ | |
| int resultCount=0; | |
| Set<Integer> primemul=new HashSet<Integer>(); | |
| int [] primes=new int[N+1]; | |
| int index=2; | |
| while(index<=N){ | |
| if(primes[index]==0){ | |
| primemul.add(index); | |
| int multiples=index*index; | |
| while(multiples<=N&&multiples>2){ | |
| if(primes[multiples]==0){ | |
| primes[multiples]=index; | |
| } | |
| multiples+=index; | |
| } | |
| }else{ | |
| if(!primemul.isEmpty() && index>=start){ | |
| int check=(int)index/primes[index]; | |
| if(primemul.contains(check)){ | |
| resultCount++; | |
| } | |
| } | |
| } | |
| index++; | |
| } | |
| return resultCount; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment