首页 > 编程语言 > 详细

java 生成两个数之间的素数

时间:2019-12-27 15:01:37      阅读:138      评论:0      收藏:0      [点我收藏+]

 

//print all prime numbers within a number

import java.util.Scanner;

public class Numbers {

    public static void main(String[] args) {
        int beg, end;
        Scanner ip = new Scanner(System.in);
        System.out.print("Enter the beginning value: ");
        beg = ip.nextInt();
        System.out.print("Enter the ending value: ");
        end = ip.nextInt();
        for (int i = beg; i <= end; i++) {
            if (isPrime(i))
                System.out.print(i + " ");
        }
        ip.close();
    }

    private static boolean isPrime(int num) {
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }
}



OUTPUT:
Enter the beginning value: 50
Enter the ending value: 100
53 59 61 67 71 73 79 83 89 97

 

java 生成两个数之间的素数

原文:https://www.cnblogs.com/sea-stream/p/12107050.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!