Check if a number can be expressed as sum of two Perfect powers
Given a positive number N, the task is to check whether the given number N can be expressed in the form of ax + by where x and y > 1 and a and b > 0. If N can be expressed in the given form then print true otherwise print false.
Examples:
Input: N = 5
Output: true
Explanation:
5 can be expressed as 22+12
Input: N = 15
Output: false
Approach: The idea is to use the concept of perfect powers to determine whether the sum exists or not. Below are the steps:
- Create an array(say perfectPower[]) to store the numbers which are a perfect power or not.
- Now the array perfectPower[] store all the elements which are perfect power, therefore we generate all possible pair sum of all the elements in this array.
- Keep the mark of the sum calculated in the above step in an array isSum[] as it can be expressed in the form of ax + by .
- After the above steps if isSum[N] is true then print true otherwise print false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isSumOfPower( int n)
{
bool isSum[n + 1];
vector< int > perfectPowers;
perfectPowers.push_back(1);
for ( int i = 0; i < (n + 1); i++) {
isSum[i] = false ;
}
for ( long long int i = 2;
i < (n + 1); i++) {
if (isSum[i] == true ) {
perfectPowers.push_back(i);
continue ;
}
for ( long long int j = i * i;
j > 0 && j < (n + 1);
j *= i) {
isSum[j] = true ;
}
}
for ( int i = 0;
i < perfectPowers.size(); i++) {
isSum[perfectPowers[i]] = false ;
}
for ( int i = 0;
i < perfectPowers.size(); i++) {
for ( int j = i;
j < perfectPowers.size(); j++) {
int sum = perfectPowers[i]
+ perfectPowers[j];
if (sum < (n + 1))
isSum[sum] = true ;
}
}
return isSum[n];
}
int main()
{
int n = 9;
if (isSumOfPower(n)) {
cout << "true\n" ;
}
else {
cout << "false\n" ;
}
}
|
Java
import java.util.*;
class GFG{
static boolean isSumOfPower( int n)
{
boolean []isSum = new boolean [n + 1 ];
Vector<Integer> perfectPowers = new Vector<Integer>();
perfectPowers.add( 1 );
for ( int i = 0 ; i < (n + 1 ); i++)
{
isSum[i] = false ;
}
for ( int i = 2 ; i < (n + 1 ); i++)
{
if (isSum[i] == true )
{
perfectPowers.add(i);
continue ;
}
for ( int j = i * i;
j > 0 && j < (n + 1 );
j *= i)
{
isSum[j] = true ;
}
}
for ( int i = 0 ;
i < perfectPowers.size();
i++)
{
isSum[perfectPowers.get(i)] = false ;
}
for ( int i = 0 ;
i < perfectPowers.size();
i++)
{
for ( int j = i;
j < perfectPowers.size();
j++)
{
int sum = perfectPowers.get(i) +
perfectPowers.get(j);
if (sum < (n + 1 ))
isSum[sum] = true ;
}
}
return isSum[n];
}
public static void main(String[] args)
{
int n = 9 ;
if (isSumOfPower(n))
{
System.out.print( "true\n" );
}
else
{
System.out.print( "false\n" );
}
}
}
|
Python3
def isSumOfPower(n):
isSum = [ 0 ] * (n + 1 )
perfectPowers = []
perfectPowers.append( 1 )
for i in range (n + 1 ):
isSum[i] = False
for i in range ( 2 , n + 1 ):
if (isSum[i] = = True ):
perfectPowers.append(i)
continue
j = i * i
while (j > 0 and j < (n + 1 )):
isSum[j] = True
j * = i
for i in range ( len (perfectPowers)):
isSum[perfectPowers[i]] = False
for i in range ( len (perfectPowers)):
for j in range ( len (perfectPowers)):
sum = (perfectPowers[i] +
perfectPowers[j])
if ( sum < (n + 1 )):
isSum[ sum ] = True
return isSum[n]
n = 9
if (isSumOfPower(n)):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool isSumOfPower( int n)
{
bool []isSum = new bool [n + 1];
List< int > perfectPowers = new List< int >();
perfectPowers.Add(1);
for ( int i = 0; i < (n + 1); i++)
{
isSum[i] = false ;
}
for ( int i = 2; i < (n + 1); i++)
{
if (isSum[i] == true )
{
perfectPowers.Add(i);
continue ;
}
for ( int j = i * i;
j > 0 && j < (n + 1);
j *= i)
{
isSum[j] = true ;
}
}
for ( int i = 0;
i < perfectPowers.Count;
i++)
{
isSum[perfectPowers[i]] = false ;
}
for ( int i = 0;
i < perfectPowers.Count;
i++)
{
for ( int j = i;
j < perfectPowers.Count;
j++)
{
int sum = perfectPowers[i] +
perfectPowers[j];
if (sum < (n + 1))
isSum[sum] = true ;
}
}
return isSum[n];
}
public static void Main(String[] args)
{
int n = 9;
if (isSumOfPower(n))
{
Console.Write( "true\n" );
}
else
{
Console.Write( "false\n" );
}
}
}
|
Javascript
<script>
function isSumOfPower(n)
{
let isSum = Array(n+1).fill(0);
let perfectPowers = [];
perfectPowers.push(1);
for (let i = 0; i < (n + 1); i++)
{
isSum[i] = false ;
}
for (let i = 2; i < (n + 1); i++)
{
if (isSum[i] == true )
{
perfectPowers.push(i);
continue ;
}
for (let j = i * i;
j > 0 && j < (n + 1);
j *= i)
{
isSum[j] = true ;
}
}
for (let i = 0;
i < perfectPowers.length;
i++)
{
isSum[perfectPowers[i]] = false ;
}
for (let i = 0;
i < perfectPowers.length;
i++)
{
for (let j = i;
j < perfectPowers.length;
j++)
{
let sum = perfectPowers[i] +
perfectPowers[j];
if (sum < (n + 1))
isSum[sum] = true ;
}
}
return isSum[n];
}
let n = 9;
if (isSumOfPower(n))
{
document.write( "true\n" );
}
else
{
document.write( "false\n" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
02 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...