Tuesday, September 17, 2024
Google search engine
HomeData Modelling & AICheck whether a number is Non-hypotenuse number

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 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++




// 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;
}


Java




// 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);
    }
}


Python3




# 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 // i
             
    # 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


C#




// 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);
    }
}


PHP




<?php
// PHP program to check if a given number
// is Non-Hypotenuse number or not.
 
// Function to find prime factor and check
// if it is of the form 4k+1 or not
function 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 = 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;
}
 
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
?>


Javascript




// JavaScript program to check if
// a given number is
// Non-Hypotenuse number or not.
 
// Function to find prime factor
// and check if it is of the form
// 4k+1 or not
function 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 = Math.floor(n / 2);
    }
 
    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (var 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 = Math.floor(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;
}
 
function test(n)
{
    process.stdout.write("Testing for " + n + " : ");
 
    if (isNonHypotenuse(n))
        console.log("YES");
 
    else
        console.log("NO");
}
 
// Driver code
let n = 11;
test(n);
 
n = 10;
test(n);
 
// This code is contributed by phasing17


Output:

Testing for 11 : YES
Testing for 10 : NO

Time complexity: O(sqrt(n)*logn)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments