Created
November 16, 2014 02:01
-
-
Save tshrkmd/44e175ffeae16c929cd2 to your computer and use it in GitHub Desktop.
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; | |
/** | |
* 素数計算プログラム | |
* @author kerukerupappa | |
* | |
*/ | |
public class Sosu { | |
private static ArrayList mSosuList = new ArrayList(); | |
/** | |
* エントリーポイント | |
* @param args | |
*/ | |
public static void main(String [] args){ | |
calcSosu(10001); | |
System.out.println( "1000番目の素数は" + getSosu(i) + "です。"); | |
} | |
/** | |
* 素数計算 | |
* @param max | |
*/ | |
private static void calcSosu(int max){ | |
int i = 3; // 奇数を計算対象にする | |
mSosuList.add(2); // 2は素数、以後2の倍数は外す | |
while( mSosuList.size() < max){ | |
boolean addSosuFlg = true; | |
for(Integer sosu : mSosuList){ | |
if( ( i % sosu ) == 0 ){ | |
addSosuFlg = false; | |
break; | |
} | |
} | |
if( addSosuFlg ){ | |
mSosuList.add(i); | |
} | |
i+=2; | |
} | |
} | |
/** | |
* 素数を返却する | |
* @param no | |
* @return | |
*/ | |
public static int getSosu(int no){ | |
if( mSosuList.size() < no ) return -1; | |
return mSosuList.get( no ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment