Check whether a number is Non-hypotenuse number
Last Updated :
29 Aug, 2022
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 numbers 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, a 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
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.
An 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 is of form 4k+1 or not.
- Print ‘YES’ if none of the factors is of the form 4k+1 Else print ‘NO’
To read more about the method of calculating the prime factor of any number, refer to this.
Below is the implementation of the above approach-
C++
#include <bits/stdc++.h>
using namespace std;
bool isNonHypotenuse( int n)
{
while (n % 2 == 0) {
n = n / 2;
}
for ( int i = 3; i <= sqrt (n); i = i + 2) {
if (n % i == 0) {
if ((i - 1) % 4 == 0)
return false ;
while (n % i == 0) {
n = n / i;
}
}
}
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" ;
}
int main()
{
int n = 11;
test(n);
n = 10;
test(n);
return 0;
}
|
Java
class GFG {
static boolean isNonHypotenuse( int n)
{
while (n % 2 == 0 ) {
n = n / 2 ;
}
for ( int i = 3 ; i <= Math.sqrt(n); i = i + 2 ) {
if (n % i == 0 ) {
if ((i - 1 ) % 4 == 0 )
return false ;
while (n % i == 0 ) {
n = n / i;
}
}
}
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" );
}
public static void main(String args[])
{
int n = 11 ;
test(n);
n = 10 ;
test(n);
}
}
|
Python3
from math import sqrt
def isNonHypotenuse(n) :
while (n % 2 = = 0 ) :
n = n / / 2
for i in range ( 3 , int (sqrt(n)) + 1 , 2 ) :
if (n % i = = 0 ) :
if ((i - 1 ) % 4 = = 0 ) :
return False
while (n % i = = 0 ) :
n = n / / i
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" )
if __name__ = = "__main__" :
n = 11
test(n)
n = 10
test(n)
|
C#
using System;
class GFG {
static bool isNonHypotenuse( int n)
{
while (n % 2 == 0) {
n = n / 2;
}
for ( int i = 3; i <= Math.Sqrt(n); i = i + 2) {
if (n % i == 0) {
if ((i - 1) % 4 == 0)
return false ;
while (n % i == 0) {
n = n / i;
}
}
}
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" );
}
public static void Main()
{
int n = 11;
test(n);
n = 10;
test(n);
}
}
|
PHP
<?php
function isNonHypotenuse( $n )
{
while ( $n % 2 == 0)
{
$n = $n / 2;
}
for ( $i = 3; $i <= sqrt( $n ); $i = $i + 2)
{
if ( $n % $i == 0)
{
if (( $i - 1) % 4 == 0)
return false;
while ( $n % $i == 0)
{
$n = $n / $i ;
}
}
}
if ( $n > 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" ;
}
$n = 11;
test( $n );
$n = 10;
test( $n );
?>
|
Javascript
function isNonHypotenuse(n)
{
while (n % 2 == 0) {
n = Math.floor(n / 2);
}
for ( var i = 3; i <= Math.sqrt(n); i = i + 2) {
if (n % i == 0) {
if ((i - 1) % 4 == 0)
return false ;
while (n % i == 0) {
n = Math.floor(n / i);
}
}
}
if (n > 2 && (n - 1) % 4 == 0)
return false ;
else
return true ;
}
function test(n)
{
process.stdout.write( "Testing for " + n + " : " );
if (isNonHypotenuse(n))
console.log( "YES" );
else
console.log( "NO" );
}
let n = 11;
test(n);
n = 10;
test(n);
|
Output:
Testing for 11 : YES
Testing for 10 : NO
Time complexity: O(sqrt(n)*logn)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...