生活随笔
收集整理的這篇文章主要介紹了
求出100~200之间的素数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
求出100~200之間的素數的個數,并求出所有的素數。
分析:素數定義是只能被1和該數本身整除
package com.math.forth;
/*** 求出100~200之間的素數的個數,并求出所有的素數。 * 分析:素數定義是只能被1和該數本身整除* * @author wql**/
public class Math02 {public static void main(String[] args) {
int sum1 = method();System.out.println(
"\n一共有" + sum1 +
"個素數");System.out.println(
"------------method2---------------");
int sum2 =
0;
for (
int i =
100; i <=
200; i++) {
if (method2(i)) {sum2++;System.out.print(i +
" ");}}System.out.println(
"\n一共有" + sum2 +
"個素數");System.out.println(
"------------method3---------------");
int sum3 =
0;
for (
int i =
100; i <=
200; i++) {
if (method3(i)) {sum3++;System.out.print(i +
" ");}}System.out.println(
"\n一共有" + sum3 +
"個素數");}
public static int method() {
int sum =
0;
for (
int i =
100; i <=
200; i++) {
for (
int j =
2; j < i; j++) {
if (i % j ==
0) {
break;}
if (j == i -
1) {System.out.print(i +
" ");sum++;}}}
return sum;}
public static boolean method2(
int i) {
for (
int j =
2; j < i; j++) {
if (i % j ==
0) {
return false;}}
return true;}
/**
*數量級灰常大,運算能力強
*/public static boolean method3(
long n) {
for (
long i =
2; i * i <= n; ++i) {
if (n % i ==
0) {
return false;}}
return true;}
}
推薦文章:http://blog.csdn.net/snow_me/article/details/52588819
轉載于:https://www.cnblogs.com/wangqilong/p/9417543.html
總結
以上是生活随笔為你收集整理的求出100~200之间的素数的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。