run in this way,   no why,   only for you heart
CSDN博客(点击进入) CSDN
51CTO(点击进入) 51CTO

求出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

posted @   _小龙人  阅读(541)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 开发者新选择:用DeepSeek实现Cursor级智能编程的免费方案
· Tinyfox 发生重大改版
· 独立开发经验谈:如何通过 Docker 让潜在客户快速体验你的系统
· 小米CR6606,CR6608,CR6609 启用SSH和刷入OpenWRT 23.05.5
· 近期最值得关注的AI技术报告与Agent综述!
点击右上角即可分享
微信分享提示