-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeFactors.cpp
More file actions
61 lines (56 loc) · 1.28 KB
/
PrimeFactors.cpp
File metadata and controls
61 lines (56 loc) · 1.28 KB
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
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <string>
#include <sstream>
#include <climits>
#include<iomanip>
using namespace std;
vector<int> prime_factors(long n) {
vector<int> factors;
if (n < 2) {
return factors;
}
while (n % 2 == 0) {
factors.push_back(2);
n /= 2;
}
for (long i = 3; i <= sqrt(n); i += 2) {
while (n % i == 0) {
factors.push_back(i);
n /= i;
}
}
if (n > 2) {
factors.push_back(n);
}
return factors;
}
int main()
{
prime_factors(3);
return 0;
}
/*Description:
Prime Factors
Inspired by one of Uncle Bob's TDD Kata
Write a function that generates factors for a given number.
The function takes an integer as parameter and returns a list of integers (ObjC: array of NSNumbers representing integers). That list contains the prime factors in numerical sequence.
Examples
1 ==> []
3 ==> [3]
8 ==> [2, 2, 2]
9 ==> [3, 3]
12 ==> [2, 2, 3]
Algorithms*/
// /> フ
// | n n 彡
// /`ミ_xノ
// / |
// / ヽ ノ
// │ | | |
// / ̄| | | |
// | ( ̄ヽ__ヽ_)__)
// \二つ
// ITS CAT FOR YOU