This Python code checks if a given number is a sunny number. A number đť‘› is considered a sunny number if đť‘›+1 is a perfect square. The perfectSquare
function determines if a number is a perfect square by checking if its square root is an integer. The isSunnyNumber
function uses this to verify if đť‘›+1 is a perfect square and prints the result. The user is prompted to enter a number, and the program then outputs whether the number is a sunny number or not.
import math
# Function to check if a number is a perfect square
def perfectSquare(n):
# Calculate the square root of the number
square_root = math.sqrt(n)
# Check if the square root is an integer (perfect square)
return((square_root - math.floor(square_root)) == 0)
# Function to check if a number is a sunny number
def isSunnyNumber(n):
# Check if n+1 is a perfect square
if (perfectSquare(n + 1)):
# If true, print that the number is a sunny number
print(str(n) + " is a sunny number!")
else:
# If false, print that the number is not a sunny number
print(str(n) + " is not a sunny number!")
# Prompt the user to enter a number
print("Enter a number to check for sunny number:")
# Read the input and convert it to an integer
sunnyNumber = int(input())
# Check if the entered number is a sunny number
isSunnyNumber(sunnyNumber)
Enter a number to check for sunny number:
48
48 is a sunny number!
import math
import math
imports the Python math module, which provides mathematical functions, including math.sqrt
for calculating the square root of a number and math.floor
for getting the largest integer less than or equal to a given number.def perfectSquare(n):
square_root = math.sqrt(n)
return((square_root - math.floor(square_root)) == 0)
def perfectSquare(n):
defines a function named perfectSquare
that takes an integer n
as its argument.square_root = math.sqrt(n)
computes the square root of n
and assigns it to the variable square_root
.math.floor(square_root)
computes the largest integer less than or equal to square_root
.square_root - math.floor(square_root)
calculates the difference between square_root
and its floored value. If n
is a perfect square, this difference will be 0 because square_root
will be an integer.return((square_root - math.floor(square_root)) == 0)
returns True
if the difference is 0 (indicating n
is a perfect square) and False
otherwise.def isSunnyNumber(n):
if (perfectSquare(n + 1)):
print(str(n) + " is a sunny number!")
else:
print(str(n) + " is not a sunny number!")
def isSunnyNumber(n):
defines a function named isSunnyNumber
that takes an integer n
as its argument.n+1
is a Perfect Square:if (perfectSquare(n + 1)):
calls the perfectSquare
function with n + 1
as the argument. If the result is True
(indicating n + 1
is a perfect square), it executes the following block.print(str(n) + " is a sunny number!")
prints that n
is a sunny number.else:
executes the following block if perfectSquare(n + 1)
returns False
.print(str(n) + " is not a sunny number!")
prints that n
is not a sunny number.print("Enter a number to check for sunny number:")
sunnyNumber = int(input())
print("Enter a number to check for sunny number:")
prints a message asking the user to enter a number.sunnyNumber = int(input())
reads the user input, converts it to an integer, and assigns it to the variable sunnyNumber
.isSunnyNumber(sunnyNumber)
isSunnyNumber(sunnyNumber)
calls the isSunnyNumber
function with sunnyNumber
as the argument to check if it is a sunny number and print the result.