Created
December 20, 2014 07:24
-
-
Save tomcha/27e93f6e1687e9a8b3ec to your computer and use it in GitHub Desktop.
prime number java
This file contains 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.ArrayList; | |
public class Prime{ | |
public static void main(String[] args){ | |
long start = System.currentTimeMillis(); | |
// System.out.println(2); | |
// System.out.println(3); | |
int num = 5; | |
ArrayList<Integer> array = new ArrayList<Integer>(); | |
array.add(2); | |
array.add(3); | |
int i = 1; | |
while(i < 100000){ | |
int checkedNum; | |
checkedNum = checkPrime(num, array); | |
if(checkedNum != 0){ | |
System.out.println(checkedNum); | |
} | |
num += 2; | |
i++; | |
} | |
long stop = System.currentTimeMillis(); | |
System.out.println((stop - start) + "ミリ秒"); | |
} | |
public static int checkPrime(int num, ArrayList<Integer> array){ | |
for(int i =0; i < array.size(); i++){ | |
if(num % ((Integer)array.get(i)).intValue() == 0){ | |
return 0; | |
} | |
if((Integer)array.get(i).intValue()>Math.sqrt(num)){ | |
break; | |
} | |
} | |
array.add( new Integer(num)); | |
return num; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment