Calculate primes since the first number until the 10001th

Overview

find_prime

Explaining this code:

find_prime is a function that takes the nth index to be calculated.
Note that this nth does not follow the array convention of starting at 0:

  • it is first received as an ordinal number;
  • checked if is between 1 and 10001;
  • then passed to lower functions, "converted" to array index.

Then we have _find_prime (which in a private/public context shoudn't be called by a programmer):
It does all the hardlifting, and what it's meant to do, which is calculate the desirable prime number.
We know that primes start at 2, going to 3, 5, 7... and so on; so primes are some sort of set/interval, mathematically represented by
[2, +inf)
Thus, it must be represented on code as a range, which purpose fits correctly at this situation - it starts at a well-defined point, and goes to the end, not inclusive (and if you dont specify, the compiler will treat this as an infinite loop, unless you give some function that will serve as a stop condition)

And there it goes. To iterate over this infinite interval, looking only for primes, we must use a filter function. It will take another function called is_prime to serve as predicate (personal opinion here: I think it's clearer to use functions instead of closures - it's better to read, to maintain, to keep track of the program flow; I only use closures when it's needed to capture some environment variable, which it's the obvious purpose of using them, it's their function!)

[to be continued]

You might also like...
🍹Branch and bound solution using Rust to calculate an optimal cocktail ingredient list of arbitrary length 🍸

Calculating an Optimal Cocktail Ingredient List Tom Explains the Problem You have 100 different ingredients You have 20 cocktails, each of which use 2

A number of collections, such as linked-lists, binary-trees, or B-Trees are most easily implemented with aliasing pointers.

StaticRc is a safe reference-counted pointer, similar to Rc or Arc, though performing its reference-counting at compile-time rather than run-time, and

Fast and easy random number generation.

alea A zero-dependency crate for fast number generation, with a focus on ease of use (no more passing &mut rng everywhere!). The implementation is bas

Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.

Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.

Number names is a Rust library to provide formatted string names for cardinal and ordinal numbers.

Number Names Number names is a Rust library to provide formatted string names for cardinal and ordinal numbers. At this time, only American English is

File Tree Fuzzer allows you to create a pseudo-random directory hierarchy filled with some number of files.

FTZZ File Tree Fuzzer allows you to create a pseudo-random directory hierarchy filled with some number of files. Installation $ cargo +nightly install

📘 Utilities for the Fibonacci Number and Sequence

Fibora Port of fibonacci-deno for Rust. Utilities for the Fibonacci Number and Sequence. Usage This package exposes two Functions, fibonacci and fibon

Outputs a random number of 🌈's
Outputs a random number of 🌈's

Rainbows Outputs a random number of 🌈 's. Rust implementation of rainbows. 📦 Installation Install Rust and Cargo Run cargo install rainbows $ rainbo

 Generating the nth Fibonacci number
Generating the nth Fibonacci number

Generating the nth Fibonacci number Per Wikipedia, "In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci

A Rust attribute macro to limit a function's number of runs over a specified period of time

throttle_my_fn: A Rust attribute macro to throttle the execution of functions throttle_my_fn is a Rust attribute macro to limit a function's number of

A small random number generator hacked on top of Rust's standard library. An exercise in pointlessness.

attorand from 'atto', meaning smaller than small, and 'rand', short for random. A small random number generator hacked on top of Rust's standard libra

A Rust library for random number generation.

A Rust library for random number generation.

Some collections to store a fixed number of elements of a specific type.

This repo provide some useful data-structures, such as: Array, HashMap, HashSet, BTreeMap, BTreeSet, etc... That lives on the stack. It's a good choic

Convert number like 42 to forty-two

num2words Convert number like 42 to forty-two Usage This crate can be either used as a library or a binary. Library Example usage: use num2words::Num2

SWC Transform to prefix logs. Useful for adding file and line number to logs

SWC Transform to prefix logs. Useful for adding file and line number to logs

1️⃣ el lisp number uno - one lisp to rule them all 🏆

luno el lisp number uno luno is the one lisp to rule them all. Still experimental, do not use it in production yet. goals embeddable small size simple

Python module implemented in Rust for counting the number of one bits in a numpy array.

bit-counter Package for counting the number of one bits in a numpy array of uint8 values. Implemented as a Python module using Rust, providing high pe

A plugin to enable random number generation for the Bevy game engine.

bevy_turborand A plugin to enable random number generation for the Bevy game engine, built upon turborand. Implements ideas from Bevy's Deterministic

A command-line utility which aligns a block of text within the terminal (or a specified number of columns), written in Rust.
A command-line utility which aligns a block of text within the terminal (or a specified number of columns), written in Rust.

align: a command line utility for aligning text. ⭐ Overview Aligns text within the terminal (or a specified number of columns). The text is treated as

Comments
  • To-do: change _is_prime iteration

    To-do: change _is_prime iteration

    Using nth, despite giving an easy way to put a breakpoint on the undefined range, makes our iterator loop once more just to take the last number (the searched prime).

    Consider changing it to a for loop: keep track of how many primes where found, stop and immediately return on the last.

    enhancement 
    opened by ciroDourado 1
Owner
Ciro Dourado
code( ).optimize( ).refactor( ).repeat( );
Ciro Dourado
Find RSA primes in files

RSA Prime Finder This program searches a file for RSA primes, i.e. primes P & Q, where P*Q == N. Usage: prime-finder 0.1 Calle Svensson <calle.svensso

Calle Svensson 12 Dec 3, 2022
Waits until the exit code of a program is zero

Waitz A rust utility to wait that a program exits with 0. You need to wait for something to start up and don't know when it finishes?

Max Strübing 15 Apr 10, 2022
Tells you how many years you need to wait until your subatomic xeon crystal synchronizer has doubled in plasma inversion efficiency on the Goldberg-Moleman scale or whatever.

about Tells you how many years you need to wait until your subatomic xeon crystal synchronizer has doubled in plasma inversion efficiency on the Goldb

null 2 Dec 3, 2021
Lazy Sieve of Eratosthenes for infinitely generating primes lazily in Rust.

lazy-prime-sieve Lazy Sieve of Eratosthenes for infinitely generating primes lazily in Rust. Usage lazy-prime-sieve is a library crate. You may add it

Arindam Das 2 Jul 13, 2023
A brief implementation of a simple hello world program in rust since they said it couldn't be done.

rust-hello-world A brief blazingly fast ?? implementation of a simple hello world program in rust since they said it couldn't be done. ?? fast ?? Also

Enoki 2 Apr 16, 2022
🐳 📦 Bringing docker containers to your AUR helper since 2022

zeus Releases | CI | Issues | Installing | Building Zeus. A simple AUR helper which utilizes docker containers allowing developers and users alike to

1337 16 Dec 17, 2022
Like wee_alloc, but smaller since I used skinnier letters in the name.

lol_alloc A laughably simple wasm global_allocator. Like wee_alloc, but smaller since I used skinnier letters in the name. lol_alloc is a experimental

Craig 44 Dec 28, 2022
Library to calculate TF-IDF (Term Frequency - Inverse Document Frequency) for generic documents.

Library to calculate TF-IDF (Term Frequency - Inverse Document Frequency) for generic documents. The library provides strategies to act on objects that implement certain document traits (NaiveDocument, ProcessedDocument, ExpandableDocument).

Ferris Tseng 13 Oct 31, 2022
A tool to calculate mean and standard deviation from multiple tests using PageSpeed Insights API.

psi-sample PSI Test tool is an open source tool to assist web developers that runs Page Speed Insight test manually! Installing To install the psi-tes

Igor Brasileiro 14 Sep 10, 2022
Calculate a player's skill level using Elo, DWZ, Ingo, TrueSkill, Glicko and Glicko-2 algorithms known from their usage in chess and online games.

skillratings Skillratings allows you to calculate the player's skill instantly in 1v1 matches or after tournaments/rating periods with a list of resul

null 10 Dec 30, 2022