ACM simulation question details (2) -- simple number theory

Source: Internet
Author: User

There are many questions related to numbers, mainly examining the basic programming capabilities. If the mathematics is better, it will be helpful for solving these problems. The following questions are collected by students. I will explain them.

1. Self Numbers
Description

In 1949 the Indian mathematician D. r. kaprekar discovered a class of numbers called self-numbers. for any positive integer n, define d (n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar .) for example, d (75) = 75 + 7 + 5 = 87. given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d (n), d (n )), d (n ))),.... for example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 33, 39, 51, 57, 69, 84, 96,111,114,120,123,129,141 ,... the number n is called a generator of d (n ). in the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. there are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.

Input

No input for this problem.

Output

Write a program to output all positive self-numbers less than 10000 in increasing order, one per line.

Sample Input

Sample Output

135792031425364 | <-- a lot more numbers | 9903991499259927993899499960997199829993

There is such a formula y = d (x1x2... Xn) = x1 + x2 +... + Xn + x1x2... Xn, for example:

D (123) = 123 + 1 + 2 + 3 = 129

D (55) = 55 + 5 + 5 = 65

Here x1x2... Xn is called the generation sub of y. For example, 123 is the generation sub of 129, and 55 is the generation sub of 65. The question requires that no sub-integers are generated within 10000.

Detailed Description: traverse the generate sub-parts from 1 to 10000 to see which numbers can be generated and output which cannot be generated. The following code is for reference.

Public static void setNumbers () {int [] numbers = new int [10000]; Arrays. fill (numbers, 0); for (int I = 1; I <10000; I ++) {int temp = I + I % 10 + (I/10) % 10 + (I/100) % 10 + I/1000; if (temp <10000) numbers [temp-1] = 1 ;}for (int I = 0; I <9999; I ++) {if (numbers [I] = 0) System. out. println (I + 1);} 2. Goldbachs Conjecture
Description

In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

Every even number greater than 4 can be written as the sum of two odd prime numbers.

For example:

8 = 3 + 5. Both 3 and 5 are odd prime numbers.
20 = 3 + 17 = 7 + 13.
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof course, but it is too long to write it on the margin of this page .)

Anyway, your task is now to verify Goldbachs conjecture for all even numbers less than a million.

Input

The input will contain in one or more test cases. Each test case consists of one even integer n with 6 <= n <1000000. Input will be terminated by a value of 0 for n.

Output

For each test case, print one line of the form n = a + B, where a and B are odd primes. numbers and operators shocould be separated by exactly one blank like in the sample output below. if there is more than one pair of odd primes adding up to n, choose the pair where the difference B-a is maximized. if there is no such pair, print a line saying "Goldbachs conjecture is wrong."

Sample Input

820420 Sample Output

8 = 3 + 520 = 3 + 1742 = 5 + 37

It is estimated that an even number greater than 4 can be written as the sum of two prime numbers. Let's write a program for verification. If there is a write expression, if not, "Goldbachs conjecture is wrong." is output .". If you can write more than one sub-statement, write the pair with two numbers with a relatively large difference. For example:

42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23

Output 42 = 5 + 37. Because 37-5 is the largest.

 

Details: The question is relatively simple. You only need to test whether two numbers are prime ones. The following code is for reference.

/** 8 = 3 + 5*20 = 3 + 17*42 = 5 + 37 */public static void test (int x) {if (6 <= x & x <1000000) {if (x % 2! = 0) {System. out. println ("the input data is not an even number! ");} Else {boolean B = false; for (int I = 3; I + I <x; I ++) {if (isPrime (I) & isPrime (x-I) {System. out. println (x + "=" + I + "+" + (x-I); B = true; break ;}} if (! B) System. out. println ("Goldbachs conjecture is wrong. ") ;}} else {System. out. println ("incorrect input data range");}/** determines whether a certain number is a prime number. If yes, true */public static boolean isPrime (int x) is returned) {for (int I = 2; I * I <= x; I ++) {if (x % I = 0) return false;} return true ;} 3. Sum of Consecutive Prime Numbers
Description

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For e

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.