How to resolve the algorithm Fortunate numbers step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fortunate numbers step by step in the Wren programming language

Table of Contents

Problem Statement

A Fortunate number is the smallest integer m > 1 such that for a given positive integer n, primorial(n) + m is a prime number, where primorial(n) is the product of the first n prime numbers. For example the first fortunate number is 3 because primorial(1) is 2 and 2 + 3 = 5 which is prime whereas 2 + 2 = 4 is composite.

After sorting and removal of any duplicates, compute and show on this page the first 8 Fortunate numbers or, if your language supports big integers, the first 50 Fortunate numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fortunate numbers step by step in the Wren programming language

Source code in the wren programming language

import "./math" for Int
import "./big" for BigInt
import "./seq" for Lst
import "./fmt" for Fmt

var primes = Int.primeSieve(379)
var primorial = BigInt.one
var fortunates = []
for (prime in primes) {
    primorial = primorial * prime
    var j = 3
    while (true) {
        if ((primorial + j).isProbablePrime(5)) {
            fortunates.add(j)
            break
        }
        j = j + 2
    }
}
fortunates = Lst.distinct(fortunates).sort()
System.print("After sorting, the first 50 distinct fortunate numbers are:")
Fmt.tprint("$3d", fortunates[0..49], 10)


  

You may also check:How to resolve the algorithm Resistor mesh step by step in the Python programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Ursa programming language
You may also check:How to resolve the algorithm Test a function step by step in the zkl programming language
You may also check:How to resolve the algorithm Statistics/Normal distribution step by step in the zkl programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Erlang programming language