How to resolve the algorithm Cullen and Woodall numbers step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Cullen and Woodall numbers step by step in the Wren 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 Wren programming language
Source code in the wren programming language
import "./big" for BigInt
var cullen = Fn.new { |n| (BigInt.one << n) * n + 1 }
var woodall = Fn.new { |n| cullen.call(n) - 2 }
System.print("First 20 Cullen numbers (n * 2^n + 1):")
for (n in 1..20) System.write("%(cullen.call(n)) ")
System.print("\n\nFirst 20 Woodall numbers (n * 2^n - 1):")
for (n in 1..20) System.write("%(woodall.call(n)) ")
System.print("\n\nFirst 2 Cullen primes (in terms of n):")
var count = 0
var n = 1
while (count < 2) {
var cn = cullen.call(n)
if (cn.isProbablePrime(5)){
System.write("%(n) ")
count = count + 1
}
n = n + 1
}
System.print("\n\nFirst 12 Woodall primes (in terms of n):")
count = 0
n = 1
while (count < 12) {
var wn = woodall.call(n)
if (wn.isProbablePrime(5)){
System.write("%(n) ")
count = count + 1
}
n = n + 1
}
System.print()
/* Cullen_and_woodall_numbers_2.wren */
import "./gmp" for Mpz
var cullen = Fn.new { |n| (Mpz.one << n) * n + 1 }
var woodall = Fn.new { |n| cullen.call(n) - 2 }
System.print("First 20 Cullen numbers (n * 2^n + 1):")
for (n in 1..20) System.write("%(cullen.call(n)) ")
System.print("\n\nFirst 20 Woodall numbers (n * 2^n - 1):")
for (n in 1..20) System.write("%(woodall.call(n)) ")
System.print("\n\nFirst 5 Cullen primes (in terms of n):")
var count = 0
var n = 1
while (count < 5) {
var cn = cullen.call(n)
if (cn.probPrime(15) > 0){
System.write("%(n) ")
count = count + 1
}
n = n + 1
}
System.print("\n\nFirst 12 Woodall primes (in terms of n):")
count = 0
n = 1
while (count < 12) {
var wn = woodall.call(n)
if (wn.probPrime(15) > 0){
System.write("%(n) ")
count = count + 1
}
n = n + 1
}
System.print()
You may also check:How to resolve the algorithm I before E except after C step by step in the Erlang programming language
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the Wren programming language
You may also check:How to resolve the algorithm Permutations step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the Raku programming language
You may also check:How to resolve the algorithm Euler's constant 0.5772... step by step in the Nim programming language