Learn to Code
John F. Dumas
contact me | resume | how it works | example programs | testimonials | main page

Python - Prime Factorization


► Problem Description: There are two kinds of numbers: composite and prime. A prime number has no factors other than 1 and itself. Composite numbers can be broken down into the product of 2 or more prime numbers.

Here are some examples:

Number Prime Factorization Prime Or Composite
2 2 Prime
3 3 Prime
4 2 * 2 Composite
5 5 Prime
6 2 * 3 Composite
7 7 Prime
8 2 * 2 * 2 Composite
9 3 * 3 Composite
10 2 * 5 Composite

Note also that when the number is composite, the factors are shown in order from smallest to largest.

Write a program to generate the table above but instead of just showing the numbers from 2 to 10, show them all the way to 10,000. Below, under the Source Code section you can find the file 'output.txt' which shows the correct output for all numbers from 2 to 10,000.


► Example Output:

   Number Prime Factorization                                Prime Or Composite
   ------ -------------------                                ------------------
    2      2                                                  Prime
    3      3                                                  Prime
    4      2 * 2                                              Composite
    5      5                                                  Prime
    6      2 * 3                                              Composite
    7      7                                                  Prime
    8      2 * 2 * 2                                          Composite
   ...
    9993   3 * 3331                                           Composite
    9994   2 * 19 * 263                                       Composite
    9995   5 * 1999                                           Composite
    9996   2 * 2 * 3 * 7 * 7 * 17                             Composite
    9997   13 * 769                                           Composite
    9998   2 * 4999                                           Composite
    9999   3 * 3 * 11 * 101                                   Composite
    10000  2 * 2 * 2 * 2 * 5 * 5 * 5 * 5                      Composite


► Source Code

All files (zip file)


Back to Example Program Index


© John F. Dumas | johnfdumas@gmail.com | main page | top of page