-
Notifications
You must be signed in to change notification settings - Fork 4
/
primeMultiplication.cpp
76 lines (61 loc) · 1.5 KB
/
primeMultiplication.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
/*Napišite program, koji omogućava da unese prirodni number number i u slučaju
da number nije prime (number koji je djeljiv samo sa 1 i sa samim sobom)
potrebno ga je rastaviti na proste faktore. Svaki složeni number se može rastaviti
na proste faktore (numbereve) koji množenjem daju taj number.
48=2*2*2*2*3
100=2*2*5*5
60=2*2*3*5*/
//Wikipedia: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
#include <iostream>
using namespace std;
bool prime(int number)
{
if (number == 2 || number == 3 || number == 5 || number == 7)
return true;
else if (number % 2 == 0 || number % 3 == 0 || number % 5 == 0 || number % 7 == 0)
return false;
else
{
int root = sqrt(number);
for (int i = 5; i <= root; i += 6)
{
if (number%i == 0)
{
return false;
}
}
}
return true;
}
void output(int number) {
for (int i = 2; number>1; i++)
{
if (prime(i) == true) {
while (number%i == 0)
{
cout << i << " ";
number /= i;
}
}
}
}
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (prime(number) == true)
cout << "This is a prime number." << endl;
else
output(number);
cout << endl;
system("PAUSE");
return 0;
}
/*
I would like to thank here Selimović Adela for helping me understand the
prime number logic behind this code, which was her optimization and math logic.
*/
//
// Created by Muhamed Karajic on December 4, 2017.
// Copyright © 2017 Muhamed Karajic. All rights reserved.
//