Check if a number is a Sunny Number in Python

Mondo Technology Updated on 2024-01-31

Given a number n, if the next number n+1 of a number is a perfectly squared number of another number, then that number n is said to be a sunny number.

Algorithm:

Store the number to be detected in the variable num1. Find the next number num1 + 1 for a given number and store it in the variable num2. Use mathThe sqrt() function calculates the square root of num2 and stores it in the variable sqrt num. Calculate the square of sqrt num and store it in the variable square num. Use the if conditional statement to determine if square num and num2 are equal. If the statement is true, the output of the given number is a sunny number. If false, the output of the given number is not a sunny number.

import math

num1 = 3

num2 = num1 + 1

sqrt_num = math.sqrt(num2)

square_num = sqrt_num * sqrt_num

if square_num == num2:

print(num1, "is a sunny number. ")

else:print(num1, "Not a sunny number. ")

Algorithm:The custom function, isperfectsquare(x), checks if the number is a perfectly squared number. isperfectsquare(x), the square root of the parameter is calculated to determine whether the square root is an integer. Custom function checksunnynumber(n) to check if the number is a sunny number. checkSunnyNumber(n), call the isperfectsquare(x) function to check whether the next number is perfectly squared. If the statement is true, the output of the given number is a sunny number. If false, the output of the given number is not a sunny number.

from math import *

def isperfectsquare(x):

sr = sqrt(x)

return (sr - floor(sr)) == 0

def checksunnynumber(n):

if isperfectsquare(n + 1):

print(n, "is a sunny number. ")

else:print(n, "Not a sunny number. ")

n = 8checksunnynumber(n)

It's not easy to create an article, if you like this article, please follow, like and share it with your friends. If you have comments and suggestions, please give us feedback in the comments.

Related Pages