您的位置:首页 > 其它

算法训练 Torry的困惑(基本型)

2018-02-16 17:34 323 查看
问题描述  Torry从小喜爱数学。一天,老师告诉他,像2、3、5、7……这样的数叫做质数。Torry突然想到一个问题,前10、100、1000、10000……个质数的乘积是多少呢?他把这个问题告诉老师。老师愣住了,一时回答不出来。于是Torry求助于会编程的你,请你算出前n个质数的乘积。不过,考虑到你才接触编程不久,Torry只要你算出这个数模上50000的值。输入格式  仅包含一个正整数n,其中n<=100000。输出格式  输出一行,即前n个质数的乘积模50000的值。样例输入
1
样例输出2-------------------------------------------------------------------------------------------import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int num = 0;
int result = 1;
int i = 2;
while (num < n) {
Boolean flag = true;
for (int s = 2; s < (int)Math.sqrt(i)+1; s++) {
if (i % s == 0)
flag = false;
}

if (flag) {
result = (result*i)%50000;
num++;
}
i++;
}
System.out.println(result);
}
}import java.util.Scanner;

public class pratice28 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int num = 0;
int result = 1;
int i = 2;
while (num < n) {
Boolean flag = true;
for (int s = 2; s < (int)Math.sqrt(i)+1; s++) {
if (i % s == 0)
flag = false;
}

if (flag) {
result = (result*i)%50000;
num++;
}
i++;
}
System.out.println(result);
}
}s的范围是最大为数值的算术平方根
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: