Haskell程序:检查输入数字是否为Neon数
本教程将讨论编写一个Haskell程序来检查输入数字是否为Neon数。Haskell中的计算是数学函数。Neon数是指满足以下性质的数字:数字等于其平方数的各位数字之和。例如:9是一个Neon数,因为其平方数“81”的各位数字之和 (8+1) 等于9。
在本教程中,我们将讨论
程序:检查数字是否为Neon数。
程序:以迭代方式打印一定范围内的Neon数。
程序:以递归方式打印一定范围内的Neon数。
算法步骤
声明一个变量并用要检查的数字填充它。
实现逻辑以检查数字是否为Neon数。
打印结果。
查找数字是否为Neon数
示例
程序:检查数字是否为Neon数
-- 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)
输出
True
在上面的程序中,
我们声明了一个辅助函数`sumDigit`,它接收一个整数作为输入并返回一个整数。
定义了辅助函数`sumDigit`,其基本条件是:如果参数为零,则函数返回零。
对于其他情况,该函数将余数添加到要返回的值中,直到参数等于零,方法是使用对同一函数的递归调用,参数为“number/10”。
我们声明了一个函数`isNeon`,它接收一个整数作为参数并返回布尔值。
我们定义了函数`isNeon`,它将数字与函数`sumDigit`返回的值(参数为number^2)进行比较。如果两者相等,则函数返回`True`,否则返回`False`。
在主函数中,我们声明并填充了一个变量`num`。
我们调用了函数`Neon`,参数为`num`,并打印了结果。
以迭代方式查找给定范围内的Neon数
示例
程序:以迭代方式生成给定范围内的Neon数
-- 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)
输出
[1,9]
在上面的程序中
我们声明并定义了与前一个程序中相同的辅助函数`sumDigit`。
我们声明并定义了函数`isNeon`,如果数字实际上是Neon数,则返回`true`,否则返回`false`。
在主函数中,我们声明并初始化了上限和下限变量。
我们使用列表推导式生成给定范围内的所有数字,并检查数字是否为Neon数。
最后,我们打印了结果。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
以递归方式查找给定范围内的Neon数
示例
程序:以递归方式生成给定范围内的Neon数
-- 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)
输出
[1,9]
在上面的程序中,
我们声明并定义了与前一个程序中相同的辅助函数`sumDigit`。
我们声明并定义了函数`isNeon`,如果数字实际上是Neon数,则返回`true`,否则返回`false`。
我们声明了一个函数`generateNeon`,它接收两个整数作为参数并返回一个整数列表。在其函数定义中,我们接收两个整数`lower`和`upper`作为参数。如果`upper`小于`lower`,我们返回空列表;否则,我们检查`upper`数字是否为Neon数。如果它是Neon数,我们将递归调用的函数返回的列表与`upper`数字连接起来。如果`upper`数字不是Neon数,我们只返回带有参数`lower`和`(upper-1)`的递归调用。该函数的逻辑是从`upper`到`lower`迭代,并检查`upper`数字是否为Neon数。如果是Neon数,我们将连接并返回列表。
在主函数中,我们声明并初始化了上限和下限数字变量。最后,我们调用了函数`generateNeon`并打印了输出。
结论
在本教程中,我们学习了如何编写一个Haskell程序来检查给定数字是否为Neon数,以及如何实现一个程序来生成给定范围内的所有Neon数。