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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cullen and Woodall numbers step by step in the Nim 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 Nim programming language

Source code in the nim programming language

import std/strformat
import integers

iterator cullenNumbers(): (int, Integer) =
  var n = 1
  var p = newInteger(2)
  while true:
    yield (n , n * p + 1)
    inc n
    p = p shl 1

iterator woodallNumbers(): (int, Integer) =
  var n = 1
  var p = newInteger(2)
  while true:
    yield (n , n * p - 1)
    inc n
    p = p shl 1

echo "First 20 Cullen numbers:"
for (n, cn) in cullenNumbers():
  stdout.write &"{cn:>9}"
  if n mod 5 == 0: echo()
  if n == 20: break

echo "\nFirst 20 Woodall numbers:"
for (n, wn) in woodallNumbers():
  stdout.write &"{wn:>9}"
  if n mod 5 == 0: echo()
  if n == 20: break

echo "\nFirst 5 Cullen primes (in terms of n):"
var count = 0
for (n, cn) in cullenNumbers():
  if cn.isPrime:
    stdout.write ' ', n
    inc count
    if count == 5: break
echo()

echo "\nFirst 12 Woodall primes (in terms of n):"
count = 0
for (n, wn) in woodallNumbers():
  if wn.isPrime:
    stdout.write ' ', n
    inc count
    if count == 12: break
echo()


  

You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the Delphi programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the Avail programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Fantom programming language
You may also check:How to resolve the algorithm Soundex step by step in the Clojure programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the JavaScript programming language