Blue Bridge Cup-Algorithm Training Torry's Confusion (Basic Type)

Problem description
  Torry has loved mathematics since childhood. One day, the teacher told him that numbers like 2, 3, 5, 7... are called prime numbers. Torry suddenly thought of a question, what is the product of the first 10, 100, 1000, 10000... prime numbers? He told the teacher about the problem. The teacher was stunned, unable to answer for a moment. So Torry turned to you who knows how to program and ask you to calculate the product of the first n prime numbers. However, considering that you have only been in contact with programming not long ago, Torry only requires you to calculate the value of 50000 on this digital analog.

The input format
  only contains a positive integer n, where n<=100000.

Output format
  outputs one line, that is, the value of the product of the first n prime numbers modulo 50000.


A question about prime numbers (prime numbers). Question Find the product of the first n prime numbers.

Problem-solving ideas:

Find n prime numbers and multiply them

The first:

#include<stdio.h>
int main()
{
    
    
	long n,i,j,s=1,c=0;
	scanf("%ld",&n);
	for (i=2;i<=100000;i++)//求出这个范围内的质数
	{
    
    
		for (j=2;j<=i;j++)
		{
    
    
			if (i%j==0)//能除尽
			  break;
		}
		if (i==j)//如果是质数,他将除到最后一次,就是本身
		{
    
    
		  	s=s*i%50000;
		  	++c;
		  	if (c>=n)
		 	break;
	    	}
	}
	printf("%ld",s);
	return 0;
}


Guess you like

Origin blog.csdn.net/mjh1667002013/article/details/113407123