-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#3 prime_fact.cpp
More file actions
37 lines (28 loc) · 1.04 KB
/
#3 prime_fact.cpp
File metadata and controls
37 lines (28 loc) · 1.04 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
// largest prime factor of 600851475143
// Wrote this while on a lunchbreak without wifi, really proud of myself
#include <iostream>
using namespace std;
int fact(){
// I had to give a lot of room for the number becuase of how big it is
int long unsigned number = 600851475143;
for (int i = 2; number > 1; i++){
// check the remainder of the number by the current i value
int primeCheck = number % i;
// if there is no remainder, it's a factor of the current number
if (primeCheck == 0){
// factor the number by the current factoring number
number /= i;
// output the factors
// makes a tree of all the factors with this output
cout << number << "\t" << i << endl;
}
}
// I ran into some issues with my compilier involving the scope of i, but after using -fpermissive flag in g++ it compiled correctly
// returns the value of the largest prime factor to print in main function
return i;
}
int main(){
int fact_num = fact();
cout << "The largest prime factor of 600851475143 is " << fact_num << endl;
return 0;
}