이경수 선생님의 수학실험실

Problem 3(Largest prime factor) 본문

Project Euler

Problem 3(Largest prime factor)

(이경수) 2019. 2. 6. 16:32

Problem 3(Largest prime factor)

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

What is the largest prime factor of the number 600851475143 ?



In Python:

import time

start_time = time.time()
num = 600851475143
i = 2

while i < (num / 2) + 1:
while num % i == 0:
num = num / i
if num == 1:
num = i
break
i += 1
print(int(num))
print(time.time() - start_time, "Seconds"

Run time: 0.0020999908447265625 Seconds



In Java:


package project_euler;


public class Euler3 {

public static void main(String[] args) {

long num = 600851475143L;

int i = 2;

long startTime = System.currentTimeMillis();

while (i < num / 2 + 1) {

    while (num % i == 0) {

      num = num / i;

      if (num == 1) {

      num = i;

      break;

      }

    }

    i ++;

}

System.out.println(num);

long endTime = System.currentTimeMillis();

System.out.println((double)(endTime - startTime)/(double)1000 +"seconds");

System.out.println((endTime - startTime) +"ms");

}

}


Run time:



solution: 6857


[from Project Euler: https://projecteuler.net/problem=3]



'Project Euler' 카테고리의 다른 글

Problem 6(Sum square difference)  (0) 2019.02.08
Problem 5(Smallest multiple)  (0) 2019.02.08
Problem 4(Largest palindrome product)  (0) 2019.02.07
Problem 2(Even Fibonacci numbers)  (0) 2019.02.06
Problem 1(Multiples of 3 and 5)  (0) 2019.02.06
Comments