Given a number N, the task is to find the sum of all the perfect square divisors of numbers from 1 to N.
Input: N = 5
Explanation: N = 5
Perfect square divisors of 1 = 1.
Similarly, perfect square divisors of 2, 3 = 1.
Perfect square divisors of 4 = 1, 4.
Perfect square divisors of 5 = 1 (of course for any prime only 1 will be the perfect square divisor)
So, total sum = 1+1+1+(1+4)+1 = 9.
Input: N = 30
Input: N = 100
Naive Approach: This approach is based on the approach implemented in this article
The above problem can be solved in O(N1/k) for any Kth power divisors, where N is the number up to which we have to find the sum. This is because, in this sum, every number will contribute floor(N/p) or int(N/p) times. Thus, while iterating through these perfect powers, we just need to add [p * int(N/p)] to the sum.
Time Complexity: O(√N)
- Let us start from start = 2, find the largest range (start to end) for which floor(N/(start2)) = floor(N/(end2))
- The contribution of all perfect squares in the interval [start, end] will contribute floor(N/(start2)) times, hence we can do update for this range at once.
- Contribution for range [start, end] can be given as:
- How to find range?
For a given value of start, end can be found by
- Now the next range can be found by substituting start = end+1.
floor(N/(start2))*(sumUpto(end) – sumUpto(start-1))
sqrt(N/K), where K = floor(N/(start^2))
Time complexity: O(N1/3) as N/(x2) cannot take more than N1/3 different values for a fixed value of N.
Below is the implementation of the above approach:
sum of all perfect square divisors from 1 to 5 is: 9
Time Complexity: O(N1/3)
- Check if a number is a perfect square having all its digits as a perfect square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Count numbers upto N which are both perfect square and perfect cube
- Divisors of n-square that are not divisors of n
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Find sum of divisors of all the divisors of a natural number
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Check if a number is perfect square without finding square root
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Print n numbers such that their sum is a perfect square
- Count all perfect divisors of a number
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Construct an Array such that cube sum of all element is a perfect square
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Sum of all prime divisors of all the numbers in range L-R
- Check if count of even divisors of N is equal to count of odd divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Check if product of array containing prime numbers is a perfect square
- Count of subarrays having exactly K perfect square numbers
Improved By : himanshu77