How to resolve the algorithm Cullen and Woodall numbers step by step in the Haskell programming language
How to resolve the algorithm Cullen and Woodall numbers step by step in the Haskell programming language
Table of Contents
Problem Statement
A Cullen number is a number of the form n × 2n + 1 where n is a natural number. A Woodall number is very similar. It is a number of the form n × 2n - 1 where n is a natural number. So for each n the associated Cullen number and Woodall number differ by 2. Woodall numbers are sometimes referred to as Riesel numbers or Cullen numbers of the second kind.
Cullen primes are Cullen numbers that are prime. Similarly, Woodall primes are Woodall numbers that are prime. It is common to list the Cullen and Woodall primes by the value of n rather than the full evaluated expression. They tend to get very large very quickly. For example, the third Cullen prime, n == 4713, has 1423 digits when evaluated.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cullen and Woodall numbers step by step in the Haskell programming language
This Haskell code does the following:
-
findCullen :: Int -> Integer
is a function that takes an integern
as input and returns then
-th Cullen number. The Cullen number is defined asn * 2 ^ n + 1
. -
cullens :: [Integer]
is a list of the first 20 Cullen numbers. -
woodalls :: [Integer]
is a list of the first 20 Woodall numbers. The Woodall number is defined as a Cullen number minus 2. -
main :: IO ()
is the main function of the program. It prints the first 20 Cullen numbers and the first 20 Woodall numbers to the console.
Here is a step-by-step explanation of the code:
-
The
findCullen
function takes an integern
as input. -
The
findCullen
function then convertsn
to an integer using thetoInteger
function. -
The
findCullen
function then calculates then
-th Cullen number using the formulan * 2 ^ n + 1
. -
The
cullens
list is created by mapping thefindCullen
function over the range of integers from 1 to 20. -
The
woodalls
list is created by mapping a lambda function over thecullens
list. The lambda function subtracts 2 from each Cullen number to get the corresponding Woodall number. -
The
main
function prints the first 20 Cullen numbers and the first 20 Woodall numbers to the console.
Source code in the haskell programming language
findCullen :: Int -> Integer
findCullen n = toInteger ( n * 2 ^ n + 1 )
cullens :: [Integer]
cullens = map findCullen [1 .. 20]
woodalls :: [Integer]
woodalls = map (\i -> i - 2 ) cullens
main :: IO ( )
main = do
putStrLn "First 20 Cullen numbers:"
print cullens
putStrLn "First 20 Woodall numbers:"
print woodalls
You may also check:How to resolve the algorithm Poker hand analyser step by step in the Phix programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the C++ programming language
You may also check:How to resolve the algorithm Long primes step by step in the C# programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Extend your language step by step in the Idris programming language