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)
- Count numbers upto N which are both perfect square and perfect cube
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Print n numbers such that their sum is a perfect square
- Count of subarrays having exactly K perfect square numbers
- Check if product of array containing prime numbers is a perfect square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if a number is perfect square without finding square root
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Count all perfect divisors of a number
- Divisors of n-square that are not divisors of n
- Perfect Square String
- Minimum number of Square Free Divisors
- Count of square free divisors of a given number
- Perfect Square factors of a Number
- Count of subarrays whose sum is a perfect square
- Closest perfect square and its distance
- Largest number that is not a perfect square
- Check if given number is perfect square
- Largest factor of a given number which is a perfect square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : himanshu77