Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the first 10-digit prime number using lazy lists in Raku

I'm trying to get the first 10-digit prime number using the lazy lists. Here is my take based on the (already given) code for computing the prime numbers:

my @primes = 2,3,5, {first * %% none(@_), (@_[*-1] ... Inf)} ...  -> $s {$s.chars == 10};

say @primes[@primes.elems-1];

The problem with that code is that it takes too long to finish.

Is there any quick method to get that first 10-digit prime number?

like image 880
Lars Malmsteen Avatar asked Jan 18 '20 13:01

Lars Malmsteen


People also ask

How do you extract prime numbers from a list in Python?

Python Program for prime numberInitialize a for loop starting from 2 ending at the integer value of the floor of the square root of the number. Check if the number is divisible by 2. Repeat till the square root of the number is checked for. In case, the number is divisible by any of the numbers, the number is not ...

What is the first 10 digit prime in e?

Next to some nine digits numbers, this shows that 7 427 466 391 is the first 10- digit prime in e.


1 Answers

You can make it faster by:

  • Starting from the smallest 10 digit number (no point going through a bunch of candidates that would never match even if they are prime)
  • Using the fast built-in is-prime method rather than a hand-rolled algorithm

Thus getting:

say (1_000_000_000..*).first(*.is-prime)

Which produces:

1000000007

You could filter out evens too, but I suspect is-prime rejects those as fast as you'd separately test them anyway. (Also, it turns out the number is so close to the first 10-digit number that nothing aside from stating at 1_000_000_000 matters in practice.)

like image 104
Jonathan Worthington Avatar answered Sep 21 '22 15:09

Jonathan Worthington