(C++/JAVA)素数问题

此生再无相见时 提交于 2020-02-07 02:21:39

(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);
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!