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

Lars Malmsteen

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?

Jonathan Worthington

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.)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Getting the position of a digit in a number using recursion

分類Dev

I have to change my number last digit and first digit but without using functions only with integers or loops. For example from 12345 to 52341

分類Dev

Getting the last digit of number counting up

分類Dev

Showing the 10 first prime Fibonacci numbers in python

分類Dev

appending zeros to make number of 10 digit

分類Dev

Getting ContactName using phone number

分類Dev

Creating regex to extract 4 digit number from string using java

分類Dev

How channel work in using channel to find prime number problem?

分類Dev

Square every digit of a number

分類Dev

How to find first n number of prime numbers in python?unable to follow the scope of a variable?

分類Dev

Error on running a pl/sql program to find sum of first 10 prime numbers

分類Dev

Generating Random Prime Number

分類Dev

Capture first digit after decimal

分類Dev

Extract last 4-digit number from a series in R using stringr

分類Dev

Haskell Reversed Number Digit List

分類Dev

Calculating and printing the nth prime number

分類Dev

OCaml program to determine prime number

分類Dev

Determine Prime Number in Python DataFrame

分類Dev

Prime number checker with two variable

分類Dev

Python Prime number wont return

分類Dev

Improving this prime number listing implementation

分類Dev

Is there a docplex function for getting solution of more than one variables for using in lazy cut?

分類Dev

input 3 digit number and print the highest number

分類Dev

Product of prime factors of a number, less than that number

分類Dev

MyBatis: Getting affected rows number using java interface mapper

分類Dev

Checking if first digit in each element in list are the same

分類Dev

Join on whole field instead of first digit of field

分類Dev

how append a random digit at the end of First Name

分類Dev

Barcode Scanner only displaying first digit of barcode

Related 関連記事

  1. 1

    Getting the position of a digit in a number using recursion

  2. 2

    I have to change my number last digit and first digit but without using functions only with integers or loops. For example from 12345 to 52341

  3. 3

    Getting the last digit of number counting up

  4. 4

    Showing the 10 first prime Fibonacci numbers in python

  5. 5

    appending zeros to make number of 10 digit

  6. 6

    Getting ContactName using phone number

  7. 7

    Creating regex to extract 4 digit number from string using java

  8. 8

    How channel work in using channel to find prime number problem?

  9. 9

    Square every digit of a number

  10. 10

    How to find first n number of prime numbers in python?unable to follow the scope of a variable?

  11. 11

    Error on running a pl/sql program to find sum of first 10 prime numbers

  12. 12

    Generating Random Prime Number

  13. 13

    Capture first digit after decimal

  14. 14

    Extract last 4-digit number from a series in R using stringr

  15. 15

    Haskell Reversed Number Digit List

  16. 16

    Calculating and printing the nth prime number

  17. 17

    OCaml program to determine prime number

  18. 18

    Determine Prime Number in Python DataFrame

  19. 19

    Prime number checker with two variable

  20. 20

    Python Prime number wont return

  21. 21

    Improving this prime number listing implementation

  22. 22

    Is there a docplex function for getting solution of more than one variables for using in lazy cut?

  23. 23

    input 3 digit number and print the highest number

  24. 24

    Product of prime factors of a number, less than that number

  25. 25

    MyBatis: Getting affected rows number using java interface mapper

  26. 26

    Checking if first digit in each element in list are the same

  27. 27

    Join on whole field instead of first digit of field

  28. 28

    how append a random digit at the end of First Name

  29. 29

    Barcode Scanner only displaying first digit of barcode

ホットタグ

アーカイブ