我试图找到一个数的最高素因子。用C语言编写算法。
这是我的密码

#include <stdio.h>
#include <stdlib.h>

long long largestPrimeFactor(long long param);
long long squareRootOrClosest(long long param);
long long checkForHighestPrime(long long param);

long long main(){
    printf("%lld\n",largestPrimeFactor(600851475143L) );
    return EXIT_SUCCESS;

}

long long largestPrimeFactor(long long  param){
    long long k = squareRootOrClosest(param);
    long long l = checkForHighestPrime(k);
    return l;

    }

long long squareRootOrClosest(long long param){
    long long i =0;
    long long s =0;

    for (long long j = 1; j*j <param; j++){
        s = j;
    }
    return s;
}

long long checkForHighestPrime(long long param){
    long long d = 0;
    long long h = 0;

    for (long long i = 1; i <param/2; i++){

        d = param%i;

        if(d == 0){
            h = d;
        }
    }
    return h;
}

我只得到一个0
我错过了什么?

最佳答案

使用long long。并将数字编辑为600851475143L

#include <stdio.h>
#include <stdlib.h>

long largestPrimeFactor(long long param);
long squareRootOrClosest(long long param);
long checkForHighestPrime(long param);

int main(){
    printf("%I64d\n",largestPrimeFactor(600851475143) );
    return EXIT_SUCCESS;

}

long largestPrimeFactor(long long param){
    long k = squareRootOrClosest(param);
    long l = checkForHighestPrime(k);
    return l;

    }

long squareRootOrClosest(long long param){
    long s =0;

    for (long long j = 1; j*j <param; j++){
        s = j;
    }
    return s;
}

long checkForHighestPrime(long param){
    long d = 0;
    long h = 0;

    for (long i = 2; i <param/2; i++){

        d = param%i;

        if(d == 0){
            h = i;
        }
    }
    return h;
}

09-06 03:33