中級 0件解決済み 正答率0%

Largest Prime Factor

Find the largest prime that divides a positive integer.

A prime is an integer greater than 1 with no positive divisors other than 1 and itself. Find the largest prime factor of n. When n is itself prime, return n.

Implement largest_prime_factor(n).

Input

n: The integer to factor.

Output

Return its largest prime divisor.

制約

2 ≤ n ≤ 10¹². The input 1 is excluded because it has no prime factor.

例

例 1
入力:
n = 84

期待される出力:
expected = 7
入力:
long long n = 84;

期待される出力:
long long expected = 7;
入力:
long n = 84;

期待される出力:
long expected = 7;

解説

84 = 2 × 2 × 3 × 7, so the largest prime factor is 7.

例 2
入力:
n = 13

期待される出力:
expected = 13
入力:
long long n = 13;

期待される出力:
long long expected = 13;
入力:
long n = 13;

期待される出力:
long expected = 13;

解説

13 is prime, so it is its own largest prime factor.

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。