Check whether a number is Non-hypotenuse number

Given a positive integer n, the task is to check if n is a Non-hypotenuse number or not. If n is a Non-hypotenuse number then print ‘YES’ else print ‘NO’.

Non-hypotenuse number : In mathematics, a Non-hypotenuse number is a natural number whose square can not be expressed as sum of two distinct non-zero squares,
i.e a non-hypotenuse number can not be put into the form of (x2 + x2 ) or K(x2 + x2 ) ; where K, x and y are positive integers. The number 1, 2, 3, 4 are Non-hypotenuse number while 5 is not a Non-hypotenuse number.
A Non-hypotenuse number can not be the hypotenuse of the right angled triangle having integer sides.

Examples:

Input: 5
Output: YES
Explanation: 5 can be expressed as 22 + 12.

Input: 6
Output: NO
Explanation: 6 can not be expressed as sum of two different squares.

First few Non-hypotenuse numbers are-

1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 14, 16, 18, 19, 21, 22, 23, 24, 27, 28, 31, 32, 33, 36, 38, 42, 43, 44, 46, 47

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A Simple Solution to check if the given number ‘n‘ is a Non-Hypotenuse number or not is to check if any combination of squares of x and y is equal to n or not.

A Efficient Solution is based on the fact that a non-hypotenuse number do not have any prime factor of the form 4k+1.
Example

Input: 12
Output: YES
Explanation: Prime factors of 12 is 2 and 3. None of them is of the form 4k+1

Input: 10
Output: NO
Explanation: Prime factors of 10 is 2 and 5. Here 5 is of the form 4k+1

Approach

  • Find all prime factors of n
  • Check if any prime factor of is of the form 4k+1 or not.
  • Print ‘YES’ if none of the factor is of the form 4k+1
    Else print ‘NO’

To read more about method of calculating prime factor of any number, refer this.

Below is the implementation of above approach-

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check if
// a given number is
// Non-Hypotenuse number or not.
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find prime factor
// and check if it is of the form
// 4k+1 or not
  
bool isNonHypotenuse(int n)
{
    // 2 is a prime number but
    // not of the form 4k+1
    // so, keep Dividing n by 2
    // until n is divisible by 2
    while (n % 2 == 0) {
        n = n / 2;
    }
  
    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2) {
  
        // if i divides n
        // check if i is of the form
        // 4k+1 or not
  
        if (n % i == 0) {
            if ((i - 1) % 4 == 0)
                return false;
  
            // while i divides n
            // divide n by i
            // and update n
            while (n % i == 0) {
                n = n / i;
            }
        }
    }
  
    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2 && (n - 1) % 4 == 0)
        return false;
  
    else
        return true;
}
  
void test(int n)
{
    cout << "Testing for "
         << n << " : ";
  
    if (isNonHypotenuse(n))
        cout << "YES"
             << "\n";
  
    else
        cout << "NO"
             << "\n";
}
  
// Driver code
int main()
{
    int n = 11;
    test(n);
  
    n = 10;
    test(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA program to check if
// a given number is
// Non-Hypotenuse number or not.
  
class GFG {
    // Function to find prime factor
    // and check if it is of the form
    // 4k+1 or not
  
    static boolean isNonHypotenuse(int n)
    {
        // 2 is a prime number but
        // not of the form 4k+1
        // so, keep Dividing n by 2
        // until n is divisible by 2
        while (n % 2 == 0) {
            n = n / 2;
        }
  
        // n must be odd at this point. So we can skip
        // one element (Note i = i +2)
        for (int i = 3; i <= Math.sqrt(n); i = i + 2) {
  
            // if i divides n
            // check if i is of the form
            // 4k+1 or not
  
            if (n % i == 0) {
                if ((i - 1) % 4 == 0)
                    return false;
  
                // while i divides n
                // divide n by i
                // and update n
                while (n % i == 0) {
                    n = n / i;
                }
            }
        }
  
        // This condition is to handle the
        // case when n  is a prime number
        // greater than 2
        if (n > 2 && (n - 1) % 4 == 0)
            return false;
  
        else
            return true;
    }
  
    public static void test(int n)
    {
  
        System.out.println("Testing for "
                           + n + " : ");
  
        if (isNonHypotenuse(n))
            System.out.println("YES");
  
        else
            System.out.println("NO");
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        int n = 11;
        test(n);
  
        n = 10;
        test(n);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if 
# a given number is 
# Non-Hypotenuse number or not. 
  
# From math lib import sqrt function
from math import sqrt
  
# Function to find prime factor 
# and check if it is of the form 
# 4k+1 or not 
def isNonHypotenuse(n) :
      
    # 2 is a prime number but not of 
    # the form 4k+1 so, keep Dividing 
    # n by 2 until n is divisible by 2 
    while (n % 2 == 0) : 
        n = n // 2
  
    # n must be odd at this point. So we 
    # can skip one element (Note i = i +2) 
    for i in range(3, int(sqrt(n)) + 1, 2) :
  
        # if i divides n check if i 
        # is of the form 4k+1 or not 
        if (n % i == 0) :
            if ((i - 1) % 4 == 0) :
                return False
  
            # while i divides n divide n 
            # by i and update n 
            while (n % i == 0) : 
                n = n //
              
    # This condition is to handle the case 
    # when n is a prime number greater than 2 
    if (n > 2 and (n - 1) % 4 == 0) :
        return False
  
    else :
        return True
  
def test(n) :
    print("Testing for", n, ":", end = " "
  
    if (isNonHypotenuse(n)) :
        print("YES")
  
    else :
        print("NO")
  
# Driver code 
if __name__ == "__main__" :
  
    n = 11
    test(n) 
  
    n = 10
    test(n) 
  
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if
// a given number is
// Non-Hypotenuse number or not.
  
using System;
class GFG {
    // Function to find prime factor
    // and check if it is of the form
    // 4k+1 or not
  
    static bool isNonHypotenuse(int n)
    {
        // 2 is a prime number but
        // not of the form 4k+1
        // so, keep Dividing n by 2
        // until n is divisible by 2
        while (n % 2 == 0) {
            n = n / 2;
        }
  
        // n must be odd at this point. So we can skip
        // one element (Note i = i +2)
        for (int i = 3; i <= Math.Sqrt(n); i = i + 2) {
  
            // if i divides n
            // check if i is of the form
            // 4k+1 or not
  
            if (n % i == 0) {
                if ((i - 1) % 4 == 0)
                    return false;
  
                // while i divides n
                // divide n by i
                // and update n
                while (n % i == 0) {
                    n = n / i;
                }
            }
        }
  
        // This condition is to handle the 
        // case when n is a prime number 
        // greater than 2
        if (n > 2 && (n - 1) % 4 == 0)
            return false;
  
        else
            return true;
    }
  
    public static void test(int n)
    {
        Console.WriteLine("Testing for " + n + " : ");
        if (isNonHypotenuse(n))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
  
    // Driver code
    public static void Main()
    {
        int n = 11;
        test(n);
  
        n = 10;
        test(n);
    }
}

chevron_right


PHP

2 && ($n – 1) % 4 == 0)
return false;
else
return true;
}

function test($n)
{
echo “Testing for “, $n , ” : “;

if (isNonHypotenuse($n))
echo “YES”. “\n”;
else
echo “NO”. “\n”;
}

// Driver code
$n = 11;
test($n);

$n = 10;
test($n);

// This code is contributed by Sach_Code
?>

Output:

Testing for 11 : YES
Testing for 10 : NO


My Personal Notes arrow_drop_up