Count of numbers in range [L, R] which can be represented as sum of two perfect powers
Given a range [L, R], the task is to find the count of numbers in the range [L, R] that can be expressed as a sum of two perfect powers.
Examples:
Input: L = 0, R = 1
Output: 2
Explanation:
The valid numbers are:
- 1 as it can be expressed as, 1 = 12 + 02.
- 0 as it can be expressed as, 0 = 02 + 02.
Therefore, the count of such numbers is 2.
Input: L = 5, R = 8
Output: 2
Explanation:
The valid numbers are:
- 5 as it can be expressed as, 5 = 12 + 22.
- 8 as it can be expressed as, 0 = 02 + 23.
Therefore, the count of such numbers is 2.
Approach: The given problem can be solved by using some mathematical observations. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int TotalPerfectPowerSum( long long L,
long long R)
{
vector< long long > pows;
pows.push_back(0);
pows.push_back(1);
for ( int p = 2; p < 25; p++) {
long long int num = 2;
while (( long long int )( pow (num, p) + 0.5) <= R) {
pows.push_back(
( long long int )( pow (num, p) + 0.5));
num++;
}
}
int ok[R + 1];
memset (ok, 0, sizeof (ok));
for ( int i = 0;
i < pows.size(); i++) {
for ( int j = 0;
j < pows.size(); j++) {
if (pows[i] + pows[j] <= R
and pows[i] + pows[j] >= L) {
ok[pows[i] + pows[j]] = 1;
}
}
}
for ( int i = 0; i <= R; i++) {
ok[i] += ok[i - 1];
}
return ok[R] - ok[L - 1];
}
signed main()
{
int L = 5, R = 8;
cout << TotalPerfectPowerSum(L, R);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int TotalPerfectPowerSum( int L, int R)
{
ArrayList<Integer> pows = new ArrayList<Integer>();
pows.add( 0 );
pows.add( 1 );
for ( int p = 2 ; p < 25 ; p++) {
int num = 2 ;
while (( int )(Math.pow(num, p) + 0.5 ) <= R) {
pows.add(( int )(Math.pow(num, p) + 0.5 ));
num++;
}
}
int [] ok = new int [R + 2 ];
for ( int i = 0 ; i < pows.size(); i++) {
for ( int j = 0 ; j < pows.size(); j++) {
if (pows.get(i) + pows.get(j) <= R
&& pows.get(i) + pows.get(j) >= L) {
ok[pows.get(i) + pows.get(j)] = 1 ;
}
}
}
for ( int i = 1 ; i <= R; i++) {
ok[i] += ok[i - 1 ];
}
return ok[R] - ok[L - 1 ];
}
public static void main(String args[])
{
int L = 5 , R = 8 ;
System.out.print(TotalPerfectPowerSum(L, R));
}
}
|
Python3
def TotalPerfectPowerSum(L, R):
pows = []
pows.append( 0 )
pows.append( 1 )
for p in range ( 2 , 25 ):
num = 2
while (( int )( pow (num, p) + 0.5 ) < = R):
pows.append(( int )( pow (num, p) + 0.5 ))
num = num + 1
ok = [ 0 for _ in range (R + 1 )]
for i in range ( 0 , int ( len (pows))):
for j in range ( 0 , len (pows)):
if (pows[i] + pows[j] < = R and pows[i] + pows[j] > = L):
ok[pows[i] + pows[j]] = 1
for i in range ( 0 , R + 1 ):
ok[i] + = ok[i - 1 ]
return ok[R] - ok[L - 1 ]
if __name__ = = "__main__" :
L = 5
R = 8
print (TotalPerfectPowerSum(L, R))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int TotalPerfectPowerSum( long L, long R)
{
List< long > pows = new List< long >();
pows.Add(0);
pows.Add(1);
for ( int p = 2; p < 25; p++) {
long num = 2;
while (( long )(Math.Pow(num, p) + 0.5) <= R) {
pows.Add(( long )(Math.Pow(num, p) + 0.5));
num++;
}
}
int [] ok = new int [R + 2];
for ( int i = 0; i < pows.Count; i++) {
for ( int j = 0; j < pows.Count; j++) {
if (pows[i] + pows[j] <= R
&& pows[i] + pows[j] >= L) {
ok[pows[i] + pows[j]] = 1;
}
}
}
for ( int i = 1; i <= R; i++) {
ok[i] += ok[i - 1];
}
return ok[R] - ok[L - 1];
}
public static void Main()
{
int L = 5, R = 8;
Console.WriteLine(TotalPerfectPowerSum(L, R));
}
}
|
Javascript
<script>
function TotalPerfectPowerSum(L,
R)
{
let pows = [];
pows.push(0);
pows.push(1);
for (let p = 2; p < 25; p++) {
let num = 2;
while (Math.floor(Math.pow(num, p) + 0.5) <= R) {
pows.push(
Math.floor(Math.pow(num, p) + 0.5));
num++;
}
}
let ok = new Array(R + 1).fill(0);
for (let i = 0;
i < pows.length; i++) {
for (let j = 0;
j < pows.length; j++) {
if (pows[i] + pows[j] <= R
&& pows[i] + pows[j] >= L) {
ok[pows[i] + pows[j]] = 1;
}
}
}
for (let i = 1; i <= R; i++) {
ok[i] += ok[i - 1];
}
return ok[R] - ok[L - 1];
}
let L = 5, R = 8;
document.write(TotalPerfectPowerSum(L, R));
</script>
|
Time Complexity: O(R*log(R))
Auxiliary Space: O(R)
Last Updated :
30 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...