-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.cpp
More file actions
60 lines (49 loc) · 1.61 KB
/
crypto.cpp
File metadata and controls
60 lines (49 loc) · 1.61 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Get the number
int n;
cin >> n;
int bigNum = 0; // Number seen the most times
int bigSeen = 0; // Amount of times the most seen number has been seen
int seen = 0; // Amount of times the current number has been seen
// While the number is even
while(n % 2 == 0)
{
seen++; // Increment the number of 2s seen
n /= 2; // Divide by 2 to remove a factor of 2
}
// If 2 has been seen
if(seen > bigSeen)
{
bigNum = 2; // Set the number seen the most times to 2
bigSeen = seen; // Set the amount of times 2 has been seen
}
// n is now odd, so consider odd factors
// Don't have to worry about non-primes as they're handled by their
// prime factors earlier on
for(int i = 3; i <= sqrt(n); i+= 2)
{
seen = 0; // Number of times i has been seen
while(n % i == 0) // While a factor of i can be taken out
{
seen++; // Increment seen
n /= i; // Remove this factor of i
}
// If i has been seen more times than any other prime
if(seen > bigSeen)
{
bigNum = i; // Set the number seen the most times to i
bigSeen = seen; // Set the amount of times i has been seen
}
}
// If there is a prime factor left, and no other prime factors have been seen
if(n > 2 && 1 > bigSeen)
{
bigNum = n; // Set the number seen the most times to n
bigSeen = 1; // Set the amount of times n has been seen to 1
}
cout << bigNum << endl;
return 0;
}