JAVA49. 判断素数个数
描述
输入两个正整数,输出这两个正整数之间(左闭右闭,即判断包括这两个整数在内有多少素数)有多少个大于2的素数。如果start>end,则将start设为end,end设为start输入描述
两个正整数输出描述
start到end之间有count个大于2的素数示例1
输入:
1 100
输出:
1到100之间有24个大于2的素数
示例2
输入:
100 1
输出:
1到100之间有24个大于2的素数
C++ 解法, 执行用时: 4ms, 内存消耗: 392KB, 提交时间: 2022-01-09
#include <iostream> #include <cmath> using namespace std; const int N=10010; int q[N]; bool st[N]; void get_prime(int n) { int j,i,cnt; cnt=0; for(i=2;i<=n;i++) { if(!st[i]) { q[cnt++]=i; for(j=i+i;j<=n;j+=i) st[j]=true; } } } bool isprime(int x) { int i; int sa=sqrt(x); for(i=2;i<=sa;i++) { if(x%i==0) return false; } return true; } int main(){ int a,i,b; cin>>a>>b; if(a>b) { int t; t=b; b=a; a=t; } //get_prime(N); int cnt=0; st[1]=true; for(i=a;i<=b;i++) { if(i<=2); else if(isprime(i)) cnt++; } printf("%d到%d之间有%d个大于2的素数",a,b,cnt); return 0; }
C++ 解法, 执行用时: 25ms, 内存消耗: 428KB, 提交时间: 2022-01-09
#include <stdio.h> int fact(int); int main(void){ int m, n, max = 0, min = 0, i = 0,l = 0,s = 0; scanf("%d %d", &m, &n); max = m > n ? m : n; min = m < n ? m : n; for(i = min; i <= max; i ++){ l = fact(i); if(l) s ++; } printf("%d到%d之间有%d个大于2的素数",min,max,s); return 0; } int fact(int n){ int i = 2; for(; i < n; i ++){ if(n % i == 0) break; } if(i == n) return 1; else return 0; }
Java 解法, 执行用时: 29ms, 内存消耗: 10700KB, 提交时间: 2021-11-17
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int start = scanner.nextInt(); int end = scanner.nextInt(); method(start,end); } public static boolean isPeime(int num){ for(int i = 2; i * i <= num; i++){ if(num % i == 0) return false; } return true; } public static void method(int start, int end) { int count=0; //write your code here...... if(start > end){ int change = start; start = end; end = change; } for(int i = start; i <= end; i++){ if(i <= 2) continue; if(isPeime(i)) count ++; } System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数"); } }
Java 解法, 执行用时: 30ms, 内存消耗: 10520KB, 提交时间: 2022-02-10
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int start = scanner.nextInt(); int end = scanner.nextInt(); method(start,end); } public static void method(int start, int end) { int count=0; //write your code here...... if(start>end){ int temp; temp=start; start=end; end=temp; } for(int i=start;i<=end;i++){ if(i>2&&issushu(i)){ count++; } } System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数"); } public static boolean issushu(int num){ for(int i=2;i*i<=num;i++){ //只要能被i整除,则不是素数 if(num%i==0){ return false; } } return true; } }
Java 解法, 执行用时: 30ms, 内存消耗: 10608KB, 提交时间: 2022-02-09
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int start = scanner.nextInt(); int end = scanner.nextInt(); method(start, end); } public static boolean isPeime(int num) { for (int i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } public static void method(int start, int end) { int count = 0; if (start > end) { int temp = start; start = end; end = temp; } for (int i = start; i <= end; i++) { if (i <= 2) { continue; } if (isPeime(i)) { count++; } } System.out.println(start + "到" + end + "之间有" + count + "个大于2的素数"); } }