Problem Description: Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49,
196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square!
Given two integers m, n (1 <= m <= n) we want to find all integers between m and n whose sum of squared divisors is itself a square. 42 is such a number.
The result will be an array of arrays, each subarray having two elements, first the number whose squared divisors is a square and then the sum of the squared divisors.
Examples:
listSquared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
listSquared(42, 250) --> [[42, 2500], [246, 84100]]
Level:6kyu
Link To Kata: Integers - Recreation One
This is a 6 Kyu kata and so a level up from the previous day's 7 Kyu. In this Kata we aim to find such integers whose sum of squares of divisors is itself a square. We start solving this Kata by making two empty arrays as num_arr and sum_arr. We loop from the starting point of given range to its ending point. For each number we push the number to num_arr and sum of square of its divisor to sum_arr.
Once we are out of the loop we initialize an empty array as ans_arr. We loop through each element of sum_arr and check if its square root after getting rounded to closest integer value when multiplied by itself i.e. after getting squared comes down to the same value of the sum_arr value. If it is then we push the value at the same index from num_arr to an empty array temp which we initialize every time the loop starts with a new index. We then also push the sum_arr value to temp. After that we push the temp array to ans_arr. We repeat this process for every number.
Please go through the code for a better understanding.
Here is a link to the code: Kata Day 38
Please mention your suggestions or your doubts in the comment below.
Happy coding. :)
No comments:
Post a Comment