-
Notifications
You must be signed in to change notification settings - Fork 1
/
prime-factors.cpp
97 lines (86 loc) · 3.14 KB
/
prime-factors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* prime-factors.cpp
*
* a program that outputs the prime factors of an inputted number
*
* Author: cmhughes
* Date: November 18th, 2016
*
* Input: user is requested for a number, n;
*
* Output: the program gives prime factors of n
*
* reference: http://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/
* Algorithm
* 1) While n is divisible by 2, print 2 and divide n by 2.
* 2) After step 1, n must be odd. Now start a loop from i = 3 to square root of n. While i divides n, print i and divide n by i, increment i by 2 and continue.
* 3) If n is a prime number and is greater than 2, then n will not become 1 by above two steps. So print n if it is greater than 2.
*
*/
#include <iostream>
#include <cassert>
#include <cmath>
int main() {
// declare the routines
int is_prime(int integer_to_be_tested);
// input from the user
int integer_for_factorisation;
// request user input, and validate
std::cout << "Enter an integer to be factored into primes:\n";
std::cin >> integer_for_factorisation;
assert(integer_for_factorisation > 0);
// if the given input is prime, then no need to go any further
if(is_prime(integer_for_factorisation)){
std::cout << "the integer you entered is prime, so the prime factorisation is:\n" << integer_for_factorisation << "*1\n";
return (0);
} else {
std::cout << "the prime factors are:\n";
// keep dividing by 2 until the quotient is not even
while(integer_for_factorisation % 2 == 0 ) {
integer_for_factorisation /= 2;
std::cout << 2 << "\n";
}
// if the quotient is prime, then stop
if(is_prime(integer_for_factorisation)){
std::cout << integer_for_factorisation << "\n";
return (0);
}
// 2) After step 1, n must be odd. Now start a loop from i = 3 to square
// root of n. While i divides n, print i and divide n by i, increment i by 2 and continue.
for(int i=3; i<=sqrt(integer_for_factorisation); i+=2){
while( (integer_for_factorisation % i)==0 ){
std::cout << i << "\n";
integer_for_factorisation /= i;
}
}
// if the quotient is prime, then stop
if(is_prime(integer_for_factorisation)){
std::cout << integer_for_factorisation << "\n";
return (0);
}
}
return (0);
}
// subroutine to test if a number is prime
int is_prime(int integer_to_be_tested){
assert(integer_to_be_tested > 0);
switch (integer_to_be_tested){
case 1:
return 1;
case 2:
return 1;
case 3:
return 1;
default:
// if the integer is even, return immediately
if(integer_to_be_tested % 2 == 0){
return 0;
}
// otherwise, proceed with the alogorithm
for(int i=2; i<=sqrt(integer_to_be_tested); i++){
if( (integer_to_be_tested % i)==0 or (integer_to_be_tested % (i+2)) ==0 ){
return 0;
}
}
return 1;
}
}