Sum of all Perfect numbers lying in the range [L, R]
Given two numbers L, R which signifies the range [L, R], the task is to find the sum of all perfect numbers lying in the range [L, R].
Examples:
Input: L = 6, R = 10
Output: 6
Explanation:
From 6 to 10, the only perfect number is 6.
Input: L = 6, R = 28
Output: 34
Explanation:
There are two perfect numbers in the range [6, 28]. They are, {6, 28}
6 + 28 = 34.
Naive Approach: The naive approach for this problem is to check if a number is a perfect number or not by iterating through every number in the range [L, R]. If there are N numbers in the range, the time complexity for this approach is O(N * sqrt(K)) where K is the maximum number(R) in the range [L, R].
Efficient Approach: The idea is to use the concept of prefix sum array to perform precomputations and store the sum of all the numbers in an array.
- Initialize an array up to the max size such that every index ‘i’ of the array represents the sum of all the perfect numbers from [0, i].
- Iterate over the array and for every index, check if it is a perfect number or not.
- If it is a perfect number, then add the sum stored at the previous index (i – 1) and the current index ‘i’.
- If it is not a perfect number, then add the sum stored at the previous index (i – 1) and the value 0.
- Finally, for every query [L, R], return the value:
sum = arr[R] - arr[L - 1]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll int
using namespace std;
long long pref[100010];
int isPerfect( int n)
{
int sum = 1;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0) {
if (i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
if (sum == n && n != 1)
return n;
return 0;
}
void precomputation()
{
for ( int i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1] + isPerfect(i);
}
}
int main()
{
int L = 6, R = 28;
precomputation();
cout << pref[R] - pref[L - 1];
return 0;
}
|
Java
class GFG {
static int pref [] = new int [ 10000 ];
static int isPerfect( int n)
{
int sum = 1 ;
for ( int i = 2 ; i * i <= n; i++) {
if (n % i == 0 ) {
if (i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
if (sum == n && n != 1 )
return n;
return 0 ;
}
static void precomputation()
{
for ( int i = 1 ; i < 10000 ; ++i) {
pref[i] = pref[i - 1 ] + isPerfect(i);
}
}
public static void main (String[] args)
{
int L = 6 , R = 28 ;
precomputation();
System.out.println(pref[R] - pref[L - 1 ]);
}
}
|
Python3
from math import sqrt
pref = [ 0 ] * 10000 ;
def isPerfect(n) :
sum = 1 ;
for i in range ( 2 , int (sqrt(n)) + 1 ) :
if (n % i = = 0 ) :
if (i * i ! = n) :
sum = sum + i + n / / i;
else :
sum = sum + i;
if ( sum = = n and n ! = 1 ) :
return n;
return 0 ;
def precomputation() :
for i in range ( 1 , 10000 ) :
pref[i] = pref[i - 1 ] + isPerfect(i);
if __name__ = = "__main__" :
L = 6 ; R = 28 ;
precomputation();
print (pref[R] - pref[L - 1 ]);
|
C#
using System;
public class GFG {
static int []pref = new int [10000];
static int isPerfect( int n)
{
int sum = 1;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0) {
if (i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
if (sum == n && n != 1)
return n;
return 0;
}
static void precomputation()
{
for ( int i = 1; i < 10000; ++i) {
pref[i] = pref[i - 1] + isPerfect(i);
}
}
public static void Main(String[] args)
{
int L = 6, R = 28;
precomputation();
Console.WriteLine(pref[R] - pref[L - 1]);
}
}
|
Javascript
<script>
var pref = Array(100010).fill(0);
function isPerfect(n)
{
var sum = 1;
for ( var i = 2; i * i <= n; i++) {
if (n % i == 0) {
if (i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
if (sum == n && n != 1)
return n;
return 0;
}
function precomputation()
{
for ( var i = 1; i <= 100000; ++i) {
pref[i] = pref[i - 1] + isPerfect(i);
}
}
var L = 6, R = 28;
precomputation();
document.write( pref[R] - pref[L - 1]);
</script>
|
Time Complexity:
- The time taken for the precomputation is O(K * sqrt(K)) where K is the number upto which we are performing the precomputation
- After precomputation, each query is answered in O(1).
Auxiliary Space: O(105)
Last Updated :
18 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...