Written Test interview 3 Splits a number into a prime factor form and how to determine whether a number is a prime number

Source: Internet
Author: User
Tags square root

Written Test interview 3 Splits a number into a prime factor form and how to determine whether a number is a prime number

Although I have already found an internship and offered an offer, I decided to brush up some simple questions.

These questions are only suitable for beginners who want to learn slag-level algorithms.


Splits a number into a prime factor, for example:

10 = 2*5 100 = 2*2*5*5

In fact, the implementation of this question is very simple. Set an I initial value to 2, and then use this number to divide it all the time, increasing the I.

The following is an implementation:

# Include <stdio. h> # include <conio. h> int main () {while (true) {printf ("\ n please enter the number: \ n"); int number; scanf ("% d", & number ); if (number <0) {// consider illegal input printf ("illegal input (<0)"); continue ;} if (number = 0 | number = 1) {// consider entering 0 or 1 printf ("% d = % d", number, number ); continue;} int I = 2; printf ("% d =", number); while (number> = I) {while (number % I = 0) {// keep Division I until division is not allowed, I + 1 printf ("% d", I); if (number! = I) printf ("*"); number = number/I;} I ++;} getch ();}
Test diagram:




How to determine whether a number is a prime number.

This is simpler.

Source code:

# Include <stdio. h> # include <conio. h> # define TRUE 1 # define FALSE-1int isPrime (int number) {// c No bool type... if (number <= 1) {// consider illegal input printf ("illegal input (<2) \ n"); return FALSE;} if (number = 2) return TRUE; int I = 2; while (I <= number) {while (number % I = 0) {if (I = number) return TRUE; else return FALSE;} I ++;} int main () {while (TRUE) {printf ("\ n please input the number: \ n"); int number; scanf ("% d", & number); if (isPrime (number) = TRUE) p Rintf ("% d is a prime number! \ N ", number); else printf (" % d not a prime number! \ N ", number);} getch ();}
Test:


However, if a number is relatively large, the time complexity will be relatively large. A better method is to use its square root.

The maximum I value is increased to sqrt (n. (The reason is that when I * I = n, I is the middle factor)

Source code:

# Include <stdio. h> # include <conio. h> # include <math. h> # define TRUE 1 # define FALSE-1int isPrime (int number) {// c No bool type... if (number <= 1) {// consider illegal input printf ("illegal input (<2) \ n"); return FALSE;} if (number = 2) return TRUE; int I = 2; int counts = 1; while (I <= sqrt (number) {if (number % I = 0) // if division, number of factors + 1 counts ++; I ++;} if (counts = 1) return TRUE; else return FALSE;} int main () {while (TRUE) {printf ("\ n enter the number \ n"); int nu Mber; scanf ("% d", & number); if (isPrime (number) = TRUE) printf ("% d is a prime number! \ N ", number); else printf (" % d not a prime number! \ N ", number);} getch ();}
Test diagram:




------------------------------------------------------------------

// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.

Reprinted please indicate the source: http://blog.csdn.net/qq844352155

Author: unparalleled

Email: coderguang@gmail.com

Yu GDUT

------------------------------------------------------------------





A natural number can be divided into three prime factor products. If the square of the three prime factors is 7950, what is the natural number?

The answer is 890. The answer process is as follows:

First, the sum of the squares of the three prime factors is an even number, so one of them must be an even number, and the even number must be 2. Therefore, we can determine that the 1st prime factor is 2.

Secondly, the single digit numbers of the other two prime factors should be selected from 1, 3, 5, 7, and 9. The single digit numbers after these five digits are 1, 9, 5, 9, and 1. The sum of the squares of the two prime factors is 7950-4 = 7946. Therefore, there must be a single digit of the prime factor of 5. At this time, we can determine that the 2nd prime factor is 5.

Finally, the square of the 3rd prime factor is 7950-4-25 = 7921, and the square is calculated as 89.

Therefore, the answer is 2*5*89 = 890.
 
Which of the following statements is correct when 18 is decomposed into a prime factor, that is, 2 × 3 × 3 = 18?

18 is decomposed into a prime factor, that is, 2 × 3 × 3 = 18. Is it true?
No doubt, of course it's wrong!
18 is a Union number. A union number can be written into the form of multiplying several prime numbers. Each prime number is called the prime factor of this Union.
A combination is expressed in the form of prime factor multiplication, which is called a decomposition prime factor.
Yes, the numbers 2, 3, and 3 are prime numbers, and the multiplication is equal to 18. However, after a sum is decomposed by short division, you must write the divisor and the final operator into the form of concatenation. Although you have already written the form of concatenation between the divisor and the final operator, however, you forgot the most important thing: it should be written as 18 = 2 × 3 × 3, rather than 2 × 3 × 3 = 18. Do you understand?

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.