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:

  1. Create an array(say perfectPower[]) to store the numbers which are a perfect power or not.
  2. 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.
  3. 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 .
  4. After the above steps if isSum[N] is true then print true otherwise print false.

#greedy #mathematical #maths #maths-power #numbers

Check if a number can be expressed as sum of two Perfect powers
1.40 GEEK