-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1sforall.cc
78 lines (67 loc) · 1.32 KB
/
1sforall.cc
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
#include <bits/stdc++.h>
using namespace std;
/*
* Prime Factorization
*
* Author: Ethan Kim
* Complexity: O(sqrt(n))
*
* Takes an integer and writes out the prime factorization in
* ascending order. Prints -1 first, when given a negative integer.
*
* Note: you can change this code to store the factors in an array or process
* the factors in other ways.
*
* Also, this code works for all integers even on INT_MIN (note that negating
* INT_MIN does nothing, but it still works because INT_MIN is a power of 2).
*
*/
#include <iostream>
using namespace std;
int factor(int n) {
int printed = 0;
int ans = 0;
long long p;
if (n == 0 || n == 1) {
cout << n << endl;
return n;
}
// if (n < 0) {
// n *= -1;
// cout << "-1" << endl;
// printed = 1;
// }
while (n % 2 == 0) {
n/=2;
ans+=2;
cout << "2" << endl;
// printed = 1;
}
for (p = 3; p*p <= n; p += 2) {
while (n % p == 0) {
n /= p;
ans+=p;
cout << p << endl;
// printed = 1;
}
}
if(n>1 && !printed) {
ans += n;
cout << "n" << n << endl;
}
return ans;
}
// int main(void) {
// int p;
// while(cin >> p && p != 0) {
// factor(p);
// }
// return 0;
// }
int main() {
int n;
cin >> n;
int ans = factor(n);
cout << "ans: " << ans << endl;
return 0;
}