Tutte le sfide
Media 0 risolte 0% di accettazione

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.

Vincoli

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

Esempi

Esempio 1
Input:
n = 84

Output atteso:
expected = 7
Input:
long long n = 84;

Output atteso:
long long expected = 7;
Input:
long n = 84;

Output atteso:
long expected = 7;

Spiegazione

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

Esempio 2
Input:
n = 13

Output atteso:
expected = 13
Input:
long long n = 13;

Output atteso:
long long expected = 13;
Input:
long n = 13;

Output atteso:
long expected = 13;

Spiegazione

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.