The C language realization of narcissus number & primes & mass factor decomposition

Source: Internet
Author: User
Tags square root

Recently, I turned to the previous C language textbooks, saw three interesting small programs, including: Looking for "narcissus number", to determine whether a number is a prime, a number of qualitative factor decomposition. I want to put these three things in a program and write down this article.

Algorithm steps

1. Find the number of daffodils.

"Narcissus number" refers to a three-digit number whose numbers are cubic and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.

2. Determine if a number is prime.

A prime number is a method that can only be divisible by 1 and itself, judging whether a number is a prime: using this number to remove the square root of 2 to this number, if divisible, it indicates that this number is not a prime, and vice versa is prime.

3. The factorization of a number by a mass factor.

For the factorization of a number n, the first prime K (starting from the smallest 2) should be found, followed by the following steps:

(1) If the prime number is equal to n, then the process of decomposing the factorization has ended.

(2) If n is not equal to K, but n can be divisible by K, then the quotient of n divided by K is used as the new positive integer n, and the first step is repeated.

(3) If n cannot be divisible by K, the first step is repeated with k+1 as the new value of K.

This program flow

This procedure is divided into three steps: The first step, the search for "Narcissus number"; The second step is to determine whether the number of daffodils found is prime; the third step, if it is not prime, the number is decomposed by mass factor.

C Program code


Compile command

This program is compiled under Linux, the compiler command is: Gcc-g-o algorithmstudyalgorithmstudy.c–lm.

Note, do not ignore the "–LM", or compile will be an error, prompted to find "sqrt".

Program Run Results

After the compilation succeeds, execute the "algorithmstudy" command with the following results:

153 is a number of daffodils .

153 not Prime .

153=3*3*17

------

370 is a number of daffodils .

370 not Prime .

370=2*5*37

------

371 is a number of daffodils .

371 not Prime .

371=7*53

------

407 is a number of daffodils .

407 not Prime .

407=11*37

------

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

My public number: ZHOUZXI, please scan the following two-dimensional code:

Narcissus & Prime number & C language implementation of mass factor decomposition

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.