blog.itcode.devblog.itcode.dev

[Programmers / JAVA] Level 1 Number of Divisors and Sum (77884)

Two integers left and right are given as parameters. Among all numbers from left to right, complete the solution function so that it adds up the numbers with an even number of divisors and subtracts the numbers with an odd number of divisors, then returns the result.

[Programmers / JAVA] Level 1 Number of Divisors and Sum (77884)

Two integers left and right are given as parameters. Among all numbers from left to right, complete the solution function so that it adds up the numbers with an even number of divisors and subtracts the numbers with an odd number of divisors, then returns the result.
RWB0104
@RWBwritten at 2021-12-15 12:29:31
Programmers

시리즈 모아보기

Programmers

19 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 Number of Divisors and Sum

Two integers left and right are given as parameters. Among all numbers from left to right, complete the solution function so that it adds up the numbers with an even number of divisors and subtracts the numbers with an odd number of divisors, then returns the result.

  • 1 ≤ leftright ≤ 1,000
leftrightresult
131743
242752

Input/Output Example #1

The following table shows all the divisors of the numbers from 13 to 17.

NumberDivisorsNumber of Divisors
131, 132
141, 2, 7, 144
151, 3, 5, 154
161, 2, 4, 8, 165
171, 172

So it must return 13 + 14 + 15 - 16 + 17 = 43.

Input/Output Example #2

The following table shows all the divisors of the numbers from 24 to 27.

NumberDivisorsNumber of Divisors
241, 2, 3, 4, 6, 8, 12, 248
251, 5, 253
261, 2, 13, 264
271, 3, 9, 274

So it must return 24 - 25 + 26 + 27 = 52.

This is a low-difficulty, simple problem. Just implementing an algorithm to find divisors is enough to solve it without much difficulty.

  1. Find the divisors.
    • We don't need to know the divisors themselves, just the count is enough.
  2. Check whether the divisor count is even or odd.
    1. Add it if even.
    2. Subtract it if odd.
  3. Return the accumulated value.

JAVA

private int[] measure(int num)
{
	ArrayList<Integer> list = new ArrayList<>();
	
	for (int i = 1; i <= Math.sqrt(num); i++)
	{
		// If it divides evenly with the given number
		if (num % i == 0)
		{
			list.add(i);
			
			// If it's not num's square root value
			if (i * i != num)
			{
				list.add(num / i);
			}
		}
	}
	
	return list.stream().sorted().mapToInt(Integer::intValue).toArray();
}

The algorithm for finding divisors is as shown above. When finding divisors, it's enough to check up to the square root of the target number.

For example, 16 has divisors [ 1, 2, 4, 8, 16 ]. Since 16=4\sqrt{16} = 4, checking only up to 4 to get [ 1, 2, 4 ] is enough to know all the divisors.

This is because 16 can be inferred from 16 / 1, and 8 from 16 / 2. When i * i != num, num has its own square root as a divisor, so unlike other numbers, only i is added without also dividing num. In the example above, 4 is that case.


After that, check the divisor count and add or subtract the value depending on whether it's even or odd.

JAVA

import java.util.ArrayList;

/**
 * Number of Divisors and Sum class
 *
 * @author RWB
 * @since 2021.12.12 Sun 00:33:56
 */
class Solution
{
	/**
	 * Method that returns the answer
	 *
	 * @param left: [int] integer 1
	 * @param right: [int] integer 2
	 *
	 * @return [int] the answer
	 */
	public int solution(int left, int right)
	{
		int answer = 0;
		
		for (int i = left; i <= right; i++)
		{
			int count = measure(i).length;
			
			answer += count % 2 == 0 ? i : -i;
		}
		
		return answer;
	}
	
	/**
	 * Method that returns divisors
	 *
	 * @param num: [int] number
	 *
	 * @return [int[]] divisors
	 */
	private int[] measure(int num)
	{
		ArrayList<Integer> list = new ArrayList<>();
		
		for (int i = 1; i <= Math.sqrt(num); i++)
		{
			// If it divides evenly with the given number
			if (num % i == 0)
			{
				list.add(i);
				
				// If it's not num's square root value
				if (i * i != num)
				{
					list.add(num / i);
				}
			}
		}
		
		return list.stream().sorted().mapToInt(Integer::intValue).toArray();
	}
}
# Programmers# Algorithm# JAVA# Level 1
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08