Python Code Example: Sunny number

This code checks if a given number is a “sunny number.” A sunny number is a number that is 1 more than a perfect square. The code first defines a function perfectSqaure that takes in a number n and returns True if n is a perfect square (i.e. if the square root of n is an integer), and False otherwise.

The code then defines another function isSunnyNumber that takes in a number n and uses the perfectSqaure function to determine if n + 1 is a perfect square. If it is, the code outputs that n is a sunny number, otherwise it outputs that n is not a sunny number.

Finally, the code prompts the user to enter a number, reads it in as sunnyNumber, and calls the isSunnyNumber function to check if it’s a sunny number.

import math

def perfectSqaure(n): 
    square_root = math.sqrt(n)
    return((square_root - math.floor(square_root)) == 0);      


def isSunnyNumber(n):
    if (perfectSqaure(n + 1)):  
        print(str(n) + " is a sunny number!")
    else:
        print(str(n) +  " is not a sunny number!")

print("Enter a number to check for sunny number:") 
sunnyNumber = int(input()) 

isSunnyNumber(sunnyNumber)
Output
Enter a number to check for sunny number:
48
48 is a sunny number!