0073-Simple Prime number problem

Source: Internet
Author: User

Simple prime number problem
Difficulty level: B; run time limit: 1000ms; operating space limit: 256000KB; code length limit: 2000000B
Question Description
The sum of the three primes is known as N, and the positive integer n is entered by the keyboard, which calculates and outputs the maximum value of the three prime number products.
Input
A positive integer n (n must be an even number in the given test data).
Output
A number that represents the maximum value of the product of three prime numbers
Input example
42
Output example
782
Other Notes
Data range: Ten <= n <= 10000.

We need to know some methods and common sense in this problem.

First, the title says "N must be an even number in the given test data". We all know that other prime numbers are odd except for 2. And this problem has three prime numbers, and must be an odd number. There must be an even number in these three indices, that is, the only even prime number 2.

Second, to determine whether a number is prime, we need to use a double loop. The outer loop to find the number you need to judge. The inner loop starts at 2, to the root of the number (as if it were mathematically done) or to the end of the number minus one (must not reach the end of the number, or any number is composite). If the outer loop finds the number divided by the current remainder of the inner loop is 0, and the number is composite, you can stop judging if the inner loop is growing to the same number that the outer loop finds, that is, the number is prime. or an initial value of 0 flag, the outer loop to find the number divided by the inner loop of the current number of 0 o'clock, flag assigned 1. It is OK to judge the value of flag after the loop is over.

After all that, how do you use it?

Code:

#include <bits/stdc++.h>using namespace Std;int i,j,k,n,a,b,c;int main () {scanf ("%d", &n); n-=2;//the topic " Given test data, n must be an even number, and the sum of the three odd numbers must be odd, so there must be 2. for (i=n/2;i<=n;i++)//And a certain difference small product large. {for (j=2;j<i;j++) if (! ( I%J)) break;//i by J Divide the description I is composite. if (j==i)//j self-increment to I description i is prime. {for (k=2;k<n-i;k++) if (! ( (n-i)%k) break;//(n-i) is divided by K (n-i) is composite. The IF (k==n-i)//k self-increment to (n-i) description (n-i) is prime. {cout<<i* (n-i) *2;//output just found the product of two and 2. break;//quit immediately. }}}return 0;}

0073-Simple Prime number problem

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.