Project Euler 3rd Question Answer

Project Euler 3. Soru Cevabı

 Project Euler 3rd Question Answer

number = 600851475143
counter = 2
while counter * counter <= number:
    if number % counter:
        counter += 1
    else:
        number //= counter
        result = number
print(result)

Here we use a while loop and ensure that the operations we need to perform continue until the process is finished.

Before the while loop, we have 2 variables: "number" and "counter". Of these, "number" represents the number whose largest prime factor we are looking for, while "counter" represents the value by which we divide the number.

For the while loop, we calculate divisor * divisor and, if it is less than or equal to our number, we create our condition for the loop to continue.

Within the while loop, we use an if condition to check the modulus of our number, and if it can be obtained (i.e., the result is 0), we increase our counter by one. If we cannot get the modulus of our number, we set our number equal to the number of times "counter" divides it. Then we set the "result" variable equal to the "number" variable, and when the loop breaks, we print it to the screen.

Note: Here, we run an algorithm that starts from the number and goes backward, thus giving results much faster compared to an algorithm that starts from zero and goes up to the number.


To see the correct answer, click here.