Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Problem description

Torry has loved mathematics since childhood. One day, the teacher told him, like two, three, five, seven… Such numbers are called prime numbers. Torry suddenly thought of a question: the first 10, 100, 1000, 10000… What is the product of primes? He told the teacher about the problem. The teacher was stunned and could not answer. So Torry turns to you, who knows how to program, and asks you to figure out the product of the first n prime numbers. But, considering you’re new to programming, Torry just needs you to figure out 50000 on this number.

Input format

Contains only one positive integer n, where n<=100000.

The output format

Output one line, the value of the product modulo 50000 of the first n primes.

Two ways, the second is more thoughtful than the first, but it’s enough to get a full score on the bluebridge Cup system.

Method one:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub		
		Scanner sc=new Scanner(System.in);
			int n=sc.nextInt();
		 caculator(n);	
	}

	private static void caculator(int n) {
		// TODO Auto-generated method stub
		int count =0;
		 int num=2;
		 int product=1;
		 while (count <n){
			 int i;
			 for(i=2; i<num; i++){if(num%i==0) {break; }}if(i==num){
				 product*=num;
				 count ++;
			 }
			 num++;
		 }
		 System.out.println(product%50000); }}Copy the code

Method 2:

import java.util.Scanner;
public class Main {
    static boolean is_prime(long n){
        for(int i=2; i*i<=n; i++){if(n%i==0)
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
   
        long n;
        Scanner sc=new Scanner(System.in);
        n=sc.nextLong();
        int sum=0,ans=1,i=2;
        while(sum<n){
            if(is_prime(i))
            {
                ans=ans*i%50000; sum++; } i++; } System.out.println(ans); }}Copy the code