Blue Bridge Cup ALGO-51 Torry's confusion (basic type) java

Problem Description

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

样例输出


2

Problem-solving ideas

Violently traverse the prime numbers, and then add them to the set one after another, and jump out when the appropriate number is reached. Finally, the sum is enough.

Reference Code

package Torry的困惑_基本型;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sr = new Scanner(System.in);
		long n =sr.nextLong();
		ArrayList<Long> list = new ArrayList<Long>();
		for (int i = 2; i < 100000; i++) {
			boolean flag = true;
			for (int j = 2; j < i; j++) {
				if ( i % j == 0) {
					flag = false;
					break;
				}
			}
			//把质数存进集合
			if (flag) {
				list.add((long) i);
				if (list.size() == n) {
				break;
			}
			}
		}
		long sum = 1;
		for (int i = 0; i < list.size(); i++) {
			sum *= list.get(i);
		}
		System.out.println(sum%50000);
	}	
}

 

Guess you like

Origin blog.csdn.net/qq_40185047/article/details/114656805