How to resolve the algorithm Cullen and Woodall numbers step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

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:

  1. findCullen :: Int -> Integer is a function that takes an integer n as input and returns the n-th Cullen number. The Cullen number is defined as n * 2 ^ n + 1.

  2. cullens :: [Integer] is a list of the first 20 Cullen numbers.

  3. woodalls :: [Integer] is a list of the first 20 Woodall numbers. The Woodall number is defined as a Cullen number minus 2.

  4. 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:

  1. The findCullen function takes an integer n as input.

  2. The findCullen function then converts n to an integer using the toInteger function.

  3. The findCullen function then calculates the n-th Cullen number using the formula n * 2 ^ n + 1.

  4. The cullens list is created by mapping the findCullen function over the range of integers from 1 to 20.

  5. The woodalls list is created by mapping a lambda function over the cullens list. The lambda function subtracts 2 from each Cullen number to get the corresponding Woodall number.

  6. 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