Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Number Theory
Four Divisors

Given an array of integers, sum the divisors of each number that has exactly four positive divisors.

Acceptance 50%
Problem Statement

Problem

You are given an integer array nums. For every number in the array, determine whether it has exactly four positive divisors.

If a number has exactly four positive divisors, add the sum of those divisors to the final answer. Otherwise, ignore that number.

Return the total sum over all qualifying numbers.

A positive divisor of n is a positive integer that divides n evenly.

Goal

Compute the sum of divisors for all values in nums that have exactly four divisors.

Input Format

  • An integer array nums.
  • Each element represents a positive integer.

Output Format

  • Return an integer: the sum of the divisors of every array element that has exactly four positive divisors.

Constraints

  • 1 <= nums.length is assumed to be small enough for per-number divisor checks.
  • Each nums[i] is a positive integer.
  • If a number does not have exactly four divisors, it contributes 0 to the result.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [21, 4, 7]

Output

32

Explanation

  • 21 has divisors [1, 3, 7, 21], so it qualifies and contributes 32.
  • 4 has divisors [1, 2, 4], so it is ignored.
  • 7 has divisors [1, 7], so it is ignored.

Total = 32.

Example 2

Input

nums = [10, 6, 8]

Output

18

Explanation

  • 10 has divisors [1, 2, 5, 10], sum = 18.
  • 6 has divisors [1, 2, 3, 6], sum = 12.
  • 8 has divisors [1, 2, 4, 8], sum = 15.

Each of these has exactly four divisors, so the total is 18 + 12 + 15 = 45.

Note: if you want a strict one-answer example, use only one qualifying value.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.