(C++/JAVA)素数问题

(C++/JAVA)输出101-200之间的所有素数,并输出素数的个数

C++:

#include<iostream>
using namespace std;
bool ISSS(int p) {
	for (int q = 2; q <= sqrt(p); q++) {
		if (p % q == 0) {
			return false;
		}
	}
	return true;
}
void SUSHU(int m, int n) {
	int num = 0;
	for (int p = m; p <= n; p++) {
		if (ISSS(p) == true) {
			num++;
			cout << p << " ";
		}
	}
	cout << endl;
	cout << num << endl;
}
int main(){
	SUSHU(101,200);
}

JAVA:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dm;
/**
 *
 * @author Lenovo
 */
public class DM {

    /**
     * @param args the command line arguments
     */
    public static boolean ISSS(int p) {
        for (int q = 2; q <= Math.sqrt(p); q++) {
            if (p % q == 0) {
                return false;
            } 
        }
        return true;
    }

    public static void SUSHU(int m, int n) {
        int num = 0;
        for (int p = m; p <= n; p++) {
            if (ISSS(p) == true) {
                num++;
                System.out.print(p+" ");
            }
        }
        System.out.println();
        System.out.println(num);
    }

    public static void main(String[] args) {
        SUSHU(101, 200);
    }
}

发布了5 篇原创文章 · 获赞 4 · 访问量 570

猜你喜欢

转载自blog.csdn.net/RY2017_Gaoxusheng/article/details/104199442