Haskell Program to Check whether the input number is a Neon Number


This tutorial will discuss writing a program to check whether the input number is a Neon Number in Haskell Programming Language. The computations in Haskell are mathematical functions. A neon Number is a number that satisfies the property: number is equal to the sum of the digits of its square. Example: 9 is a neon number because the sum of the digits of its square “81” (8+1) is equal to the number 9.

In this Tutorial, we will discuss

  • Program to check whether the number is a Neon number.

  • Program to print neon numbers in a range in an iterative way.

  • Program to print neon numbers in a range in a recursive way.

Algorithmic steps

  • Declare a variable and populate it with a number you want to check.

  • Implement the logic to check whether the number is a Neon Number or Not.

  • Print the result.

Finding Whether A Number Is A Neon Number

Example

Program to check whether a number is a Neon Number

-- function declaration for sumDigit function sumDigit :: Int->Int -- function definition for base condition sumDigit 0 = 0 -- function definition for all other cases sumDigit num = mod num 10 + sumDigit (div num 10) -- function declaration for isNeon function isNeon :: Int->Bool -- function definition for isNeon function isNeon num = num == (sumDigit (num*num)) -- main function main = do -- declaring and populating variable with a number let num = 9 -- invoking the isNeon function and printing the result print (isNeon 9)

Output

True

In the above program,

We declared a helper function sumDigit which takes an integer as an input and returns Integer.

Defined the helper function sumDigit with the base condition if the parameter is zero function returns zero.

Defined the function for other cases as adding the remainder to the value to be returned until the argument equals zero by using recursive calls to the same function with parameter as “number/10”

We declared a function isNeon which takes an integer as a parameter and returns boolean.

We defined the function isNeon as it compares the number with the value returned by the function sumDigit with argument number^2. If both are equal function returns True else the function returns False.

In the main function, we declared and populated a variable num in the main function.

We invoked the function Neon with an argument as num and printed the result.

Finding Neon Numbers In A Given Range In An Iterative Way

Example

Program to generate neon numbers in a given range in an iterative way

-- function declaration and definition for sumDigit function sumDigit :: Int->Int sumDigit 0 = 0 sumDigit num = mod num 10 + sumDigit (div num 10) -- Function declaration and definition for isNeon function isNeon :: Int->Bool isNeon num = num == (sumDigit (num*num)) main = do -- declaring and initializing upper and lower bounds with values let lower = 1 let upper = 100 -- list comprehension to generate Neon numbers in a range let list = [x | x <- [lower..upper], isNeon x] print (list)

Output

[1,9]

In the above program

We declared and defined a helper function sumDigit same as in previous program

We declared and defined function isNeon which returns true if the number is actually a neon number else it returns false.

In the main function we declared and initialized variables for upper and lower bounds.

We used a list comprehension which generates all numbers in the given range and checks the number is neon.

Finally We printed the result.

Finding Neon Numbers In A Given Range In A Recursive Way

Example

Program to generate neon numbers in a given range in a recursive way

-- function declaration for sumDigit function sumDigit :: Int->Int -- function definition for sumDigit function sumDigit 0 = 0 sumDigit num = mod num 10 + sumDigit (div num 10) -- function declaration and definition for isNeon function isNeon :: Int->Bool isNeon num = num == (sumDigit (num*num)) -- function declaration for generateNeon function generateNeon :: Int->Int->[Int] -- function definition for generateNeon function generateNeon lower upper = if (upper<lower) then [] else if(isNeon upper) then generateNeon lower (upper-1) ++ [upper] else generateNeon lower (upper-1) main = do -- declaring and initializing variable for lower and upper bounds let lower = 1 let upper = 100 print (generateNeon lower upper)

Output

[1,9]

In the above program,

We declared and defined a helper function sumDigit same as in previous program.

We declared and defined function isNeon which returns true if the number is actually a neon number else it returns false.

We declared a function generateNeon which takes two integers as arguments and returns a list of integers. In its function definition, we are taking two integers lower and upper as arguments. If the upper is less than the lower we are returning the empty list else we are checking if the upper number is a Neon. If it is a Neon number we are concatinating the returned list from a recursively called function to the upper number. If the upper number is not a Neon number, we are returning only a recursive call with the argument lower and (upper-1). The logic is this function iterates from upper to lower by checking whether the upper number is a Neon. If it is a Neon number we are concatenating and returning the list.

In the main function we declared and initialized variables for upper and lower numbers. Finally, we invoked the function generateNeon and printed the output.

Conclusion

In this tutorial, We learned to code a program to check whether a given number is a Neon Number or not in Haskell. And to implement a program to generate all the neon numbers in a given range.

Updated on: 27-Oct-2022

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements