FastPrepSum of Distinct Prime Divisors

Sum of Distinct Prime Divisors

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a positive integer n, return the sum of its distinct prime divisors.

Each prime divisor contributes once, regardless of its exponent in the factorization. For n = 1, return 0.

Function

sumDistinctPrimeDivisors(n: long) → long

Examples

Example 1

n = 12return = 5

The distinct prime divisors of 12 are 2 and 3, whose sum is 5.

Example 2

n = 13return = 13

Because 13 is prime, it is its only prime divisor.

Constraints

  • 1 <= n <= 10^12

More Microsoft problems

See Microsoft hiring insights
public long sumDistinctPrimeDivisors(long n) {
  // Write your code here.
}
n12
expected5
Checking account…