Download Zapewne znany jest Tobie fakt, że wszystkie liczby naturalne

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

List of prime numbers wikipedia , lookup

Addition wikipedia , lookup

Arithmetic wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript


You are surely familiar with the fact that all natural numbers greater than zero are divided into:
Prime numbers,
Composite numbers.
Examples of prime and composite numbers
2 – prime number
3 – prime number
4 = 2 · 2 – composite number
5 – prime number
6 = 2 · 3 – composite number
7 – prime number
8 = 2 · 2 · 2 – composite number
9 = 3 · 3 – composite number
It is easy to notice that each composite number can be presented by the use of arithmetic
product of prime numbers i.e. prime factorization.
You have surely already done prime factorization on maths lessons.
To find prime numbers of n, the n number is divided by the smallest prime number that divides
the n number. Such quotient is further divided by the smallest divisor that is a prime number. The
operations are repeated until the divisor is number 1. Thus all divisors of n that are prime
numbers are obtained.
Theorem – Fundamental theorem of arithmetic
Each natural number greater than 1 can be notated as an arithmetic product of prime numbers.
Our algorithm that factorizes number n is quite easy. It is called a direct seeking of prime
factorization. We check the divisibility of the n number by subsequent natural numbers from 2 to
the root of n. If number n can be divided by a given number from a given interval, the number will
be printed on the output and the quotient will correspond to a new n. The procedure is repeated
as long as it is possible. Then we move to seeking another divisor.
Problem specification
Input data:
n ϵ N, n > 1 – prime factorizing
Output data:
prime factors of number n ϵ N
additional data:
i ϵ N – following divisors
List of steps
[b]Step 1:[/b] Assign i = 2;
[b]Step 2:[/b] As long as i is smaller than or equal to integer part of √n
follow steps from 1 to 6.
[b]Step 3:[/b] As long as n mod i = 0
[b]Step 4:[/b] Write i
[b]Step 5:[/b] Assign n = n / i
[b]Step 6:[/b] If n = 1, go to step 8
; if n has the last factor larger than the root of n
[b]Step 7:[/b] If n > 1, write n
[b]Step 8:[/b] End
Example
Program for prime factorization.
Hide
// Prime factorization of a natural number
// primefact.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
unsigned long n;
int i = 2;
cout << "Give n: ";
cin >> n;
cin.ignore();
while (i <= (unsigned long)sqrt((double)n))
{
while (!(n % i))
{
n /= i;
cout << i << " ";
}
if (n == 1) break;
i++;
}
if (n > 1) cout << n;
cout << endl;
return 0;
}
Outcome of the program
primefact.cpp, give n