Possibly Data Type Problem

Beginner programmer here.

This is Problem 3 on Project Euler.

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?


I think I'm having trouble managing the data types (Line 13).

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
#include <iostream>
#include <cmath>
using namespace std;

int num;
int fact;
int i;
int found;
int pfact;
int remain;

int main(){
>>>>num = 600851475143LL;
    found = 0;
    pfact = 2;
    while (found == 0){
          remain = num %  pfact;
          pfact = pfact + 1;
          if (remain == 0){
              if (pfact == num){
                  found = 1;
                  }
              fact = pfact - 1;
              cout << "The factor is " << fact << endl;
              num = num / pfact;
              cout << "num = " << num << endl;
          }
    }
cout << fact;
system("pause");

}
num should be of type long long.

Note that the algorithm you've chosen:
1. Doesn't filter out non-prime divisors of num.
2. Will require over 600 billion iterations.
See if you can find a way to significantly trim down the number of iterations.
Thanks helios.
So if I plan on using a type long long, do I always declare it long long AND when include LL suffix of the number when I define the value?

Also, I don't know if I can shorten the length of the program any. It ran pretty fast on my laptop, but if you see that I could've solved the problem in a easier way, please give me a hint so I can try it.

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
#include <iostream>
#include <cmath>
using namespace std;

long long num;
int fact;
int i;
int found;
int pfact;
int remain;

int main(){
    num = 600851475143LL;
    found = 0;
    pfact = 1;
    while (found == 0){
          remain = num %  pfact;
          pfact = pfact + 2;
          if (remain == 0){
              fact = pfact - 2;
              num = num / fact;
              if (num == 1){
                  found = 1;
                  }
          }
    }
cout << "The largest prime is " << fact << endl;;
system("pause");
if it is type long long it is a given it is long long.
The only times I can think to use LL , u , L are:
1. Parameter ex:
1
2
3
4
 function( int ); //proto
function( long ); //proto
function( long long ); //proto
function( 123LL ); //calling the long long proto instead of int or long 

2. using an auto type ex: const auto a = 123LL; //this is a long long type now
I could be wrong though
For me, it prints this:
The factor is 71
num = 8345159376
The factor is 74
num = 111268791
The factor is 153
num = 722524
The factor is 16421
num = 43
and then hangs. It doesn't crash, it just sits there computing forever.
The output isn't even right, since 74 is obviously not prime, and the largest prime that divides the number is 6857, which is lower than 16421.

Also, I don't know if I can shorten the length of the program any.
Program length affects execution speed only indirectly.
My second code is the correct program. Its output is 6857.

I guess I'm concerned with learning new programming techniques since I am new. Would you have solved it with a different algorithm?
Topic archived. No new replies allowed.