2016 algorithm training--torry puzzle (basic type)

Source: Internet
Author: User
Algorithm Training Torry Puzzle (Basic type) problem description Torry from childhood love mathematics. One day, the teacher told him, like 2, 3, 5, 7 ... Such numbers are called prime numbers. Torry suddenly thought of a problem, the first 10, 100, 1000, 10000 ... What is the product of a prime number? He told the teacher the question. The teacher was stunned and couldn't answer it. So Torry turned to the programmer, and asked you to calculate the product of the first n prime numbers. However, considering that you are only in touch with programming soon, Torry as long as you figure out the value of 50000 on the modulo. The input format contains only a positive integer n, where n<=100000. The output format outputs a row, which is the value of the product modulo 50000 of the first n prime numbers. Sample input
1


Sample output

2

Code:

Import Java.util.Arrays;
Import Java.util.Scanner;

public class Main {public
	static void Main (string[] args) throws Exception {
		Scanner in = new Scanner (system.in);
		int n = in.nextint ();
		int num=0,sum=1;
		for (int i=2;num<n;i++) {
			if (is_prime (i)) {
				sum= (sum*i)%50000;
				num++;
			}
		}
		SYSTEM.OUT.PRINTLN (sum);
	}

	private static Boolean is_prime (int m) {
		//TODO auto-generated method stub for
		(int i=2;i<=math.sqrt (m); i++ {
			if (m%i==0)
				return false;
		}
		return true;
	}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.