とんちゃんといっしょ

Cloudに関する技術とか日常とかについて書いたり書かなかったり

Problem 3

問題

Find the largest prime factor of a composite number.

英語

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

What is the largest prime factor of the number 600851475143 ?

http://projecteuler.net/index.php?section=problems&id=3
日本語

13195 の素因数は 5、7、13、29 である。

600851475143 の素因数のうち最大のものを求めよ。

解説

素因数を求めるにはmathnにあるInteger#prime_divisionを使うと楽勝。
prime_divisionをすると小さい素因数から順に並んだ
[素因数, 素因数の数]
の配列が得られるので、配列の最後にある素因数を出力させておしまい。

ソースコード(Ruby)

require 'mathn'
p 600851475143.prime_division.last[0]