Dividing factorial of a number using another factorial

79 Views Asked by At

I recently came across the following code that is used to calculate prime factorial

 public static void euler3b() {
        long result, limit;
        result = limit = 8462696833;

        for (int i = 2 ; ; i++){
            if (isPrime(i) && limit%i == 0){
                result /= i;
            }
            if (result == 1){
                System.out.println(i);
                break;
            }
        }
    }

The thing that stumpted me is the line

   result /= i;

How can we make the assumption that just because limit is divisible by i, result will also be divisible by i ?

1

There are 1 best solutions below

1
On

Let the initial limit be $\ell$, whose prime factorization is: $$ \ell = p_1^{e_1} \cdots p_k^{e_k} $$ where $p_1 < \dots < p_k$. Then we are essentially taking $i$ to be each of $p_1, \ldots, p_k$ and iteratively dividing it out from $\ell$. Since $i$ already exists in the prime factorization of $\ell$ and there is no way that we would have divided it out from $\ell$ before step $i$, the result follows.