Fortunate numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(Add Factor)
m (→‎{{header|Raku}}: for want of a comma...)
Line 151: Line 151:
<lang perl6>my @primorials = [\*] grep *.is-prime, ^∞;
<lang perl6>my @primorials = [\*] grep *.is-prime, ^∞;


say display :title("First 50 distinct fortunate numbers:\n"),

say display :title("First 50 distinct fortunate numbers:\n")
(squish sort @primorials[^75].hyper.map: -> $primorial {
(squish sort @primorials[^75].hyper.map: -> $primorial {
(2..∞).first: (* + $primorial).is-prime
(2..∞).first: (* + $primorial).is-prime
})[^50];
})[^50];



sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n") {
sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n") {

Revision as of 00:15, 2 August 2021

Fortunate numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Definition

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.


Task

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.


Related task


See also



Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: grouping io kernel math math.factorials math.primes math.ranges prettyprint sequences sets sorting ;

"First 50 distinct fortunate numbers:" print 75 [1,b] [

   primorial dup next-prime 2dup - abs 1 =
   [ next-prime ] when - abs

] map members natural-sort 50 head 10 group simple-table.</lang>

Output:
First 50 distinct fortunate numbers:
3   5   7   13  17  19  23  37  47  59
61  67  71  79  89  101 103 107 109 127
151 157 163 167 191 197 199 223 229 233
239 271 277 283 293 307 311 313 331 353
373 379 383 397 401 409 419 421 439 443

Go

Translation of: Wren
Library: Go-rcu

<lang go>package main

import (

   "fmt"
   "math/big"
   "rcu"
   "sort"

)

func main() {

   primes := rcu.Primes(379)
   primorial := big.NewInt(1)
   var fortunates []int
   bPrime := new(big.Int)
   for _, prime := range primes {
       bPrime.SetUint64(uint64(prime))
       primorial.Mul(primorial, bPrime)
       for j := 3; ; j += 2 {
           jj := big.NewInt(int64(j))
           bPrime.Add(primorial, jj)
           if bPrime.ProbablyPrime(5) {
               fortunates = append(fortunates, j)
               break
           }
       }
   }
   m := make(map[int]bool)
   for _, f := range fortunates {
       m[f] = true
   }
   fortunates = fortunates[:0]
   for k := range m {
       fortunates = append(fortunates, k)
   }
   sort.Ints(fortunates)
   fmt.Println("After sorting, the first 50 distinct fortunate numbers are:")
   for i, f := range fortunates[0:50] {
       fmt.Printf("%3d ", f)
       if (i+1)%10 == 0 {
           fmt.Println()
       }
   }
   fmt.Println()

}</lang>

Output:
After sorting, the first 50 distinct fortunate numbers are:
  3   5   7  13  17  19  23  37  47  59 
 61  67  71  79  89 101 103 107 109 127 
151 157 163 167 191 197 199 223 229 233 
239 271 277 283 293 307 311 313 331 353 
373 379 383 397 401 409 419 421 439 443 

Nim

Library: bignum

Nim doesn’t provide a standard module to deal with big integers. So, we have chosen to use the third party module “bignum” which provides functions to easily find primes and check if a number is prime. <lang Nim>import algorithm, sequtils, strutils import bignum

const

 N = 50      # Number of fortunate numbers.
 Lim = 75    # Number of primorials to compute.


iterator primorials(lim: Positive): Int =

 var prime = newInt(2)
 var primorial = newInt(1)
 for _ in 1..lim:
   primorial *= prime
   prime = prime.nextPrime()
   yield primorial


var list: seq[int] for p in primorials(Lim):

 var m = 3
 while true:
   if probablyPrime(p + m, 25) != 0:
     list.add m
     break
   inc m, 2

list.sort() list = list.deduplicate(true) if list.len < N:

 quit "Not enough values. Wanted $1, got $2.".format(N, list.len), QuitFailure

list.setLen(N) echo "First $# fortunate numbers:".format(N) for i, m in list:

 stdout.write ($m).align(3), if (i + 1) mod 10 == 0: '\n' else: ' '</lang>
Output:
First 50 fortunate numbers:
  3   5   7  13  17  19  23  37  47  59
 61  67  71  79  89 101 103 107 109 127
151 157 163 167 191 197 199 223 229 233
239 271 277 283 293 307 311 313 331 353
373 379 383 397 401 409 419 421 439 443

Raku

Limit of 75 primorials to get first 50 unique fortunates is arbitrary, found through trial and error.

<lang perl6>my @primorials = [\*] grep *.is-prime, ^∞;

say display :title("First 50 distinct fortunate numbers:\n"),

  (squish sort @primorials[^75].hyper.map: -> $primorial {
      (2..∞).first: (* + $primorial).is-prime
  })[^50];

sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n") {

   cache $list;
   $title ~ $list.batch($cols)».fmt($fmt).join: "\n"

}</lang>

Output:
First 50 distinct fortunate numbers:
     3      5      7     13     17     19     23     37     47     59
    61     67     71     79     89    101    103    107    109    127
   151    157    163    167    191    197    199    223    229    233
   239    271    277    283    293    307    311    313    331    353
   373    379    383    397    401    409    419    421    439    443

Wren

Library: Wren-math
Library: Wren-big
Library: Wren-sort
Library: Wren-seq
Library: Wren-fmt

<lang ecmascript>import "/math" for Int import "/big" for BigInt import "/sort" for Sort 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.quick(fortunates) System.print("After sorting, the first 50 distinct fortunate numbers are:") for (chunk in Lst.chunks(fortunates[0..49], 10)) Fmt.print("$3d", chunk)</lang>

Output:
After sorting, the first 50 distinct fortunate numbers are:
  3   5   7  13  17  19  23  37  47  59
 61  67  71  79  89 101 103 107 109 127
151 157 163 167 191 197 199 223 229 233
239 271 277 283 293 307 311 313 331 353
373 379 383 397 401 409 419 421 439 443