[Solved] Printing largest prime factor of a composite number


The number whose factor you are trying to find is out of the range of values that long can hold.Here you can see maximum size of values that data type in c can store.Also, I would recommend using long long at all places where the number to be stored is derived from the num i.e. in your program everywhere.Make sure not to change data types unless you are sure that it won’t cross its maximum limit.
Also, how can you use

for(int i=2; i<sqrt(600851475143); i++)

in C code?Thats the C++ feature.
Also, there is no need for

#include <time.h>

so your code should look something like this:

#include <stdio.h>
#include <math.h>
//#include <time.h>

int temp;
void fact(long long a);
long long prime(long long a);

int main(){
    fact(600851475143);
}

void fact(long long num){
    long long i=2;
    for(; i<sqrt(600851475143); i++){
        if(num%i == 0){
            if(prime(i)){
                printf("%d\n", i);
            }
        }
    }
}

long long prime(long long num){
    long long i=2;
    int k = 1;
    for(; i<num; i++){
        if(num % i == 0){
            k=0;
            break;
        }
    }
    return k;
}

Hope it helps.

2

solved Printing largest prime factor of a composite number