Search
 
SCRIPT & CODE EXAMPLE
 

RUST

find prime numbers with the sieve of Eratosthenes

fn sieve(in_n: u32) -> Vec<u32> {
    let n = in_n as usize;
    let mut a = Vec::new();
    a.resize(n, true);
    for i in 2..(f64::sqrt(n as f64) as usize + 1) {
        if a[i] {
            for j in (i*i..n).step_by(i) {
                a[j] = false;
            }
        }
    }
    return a.iter().enumerate().filter_map(|(i, &t)| {
      if t {Some(i as u32)} else {None}   
    }).collect();
}

fn main() {
    let n = 12;
    println!("List of primes less than {} = {:?} ", n, sieve(n));
}
Comment

sieve of eratosthenes - prime number

# Python program to print all
# primes smaller than or equal to
# n using Sieve of Eratosthenes
 
 
def SieveOfEratosthenes(n):
 
    # Create a boolean array
    # "prime[0..n]" and initialize
    #  all entries it as true.
    # A value in prime[i] will
    # finally be false if i is
    # Not a prime, else true.
    prime = [True for i in range(n+1)]
    p = 2
    while (p * p <= n):
 
        # If prime[p] is not
        # changed, then it is a prime
        if (prime[p] == True):
 
            # Update all multiples of p
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
 
    # Print all prime numbers
    for p in range(2, n+1):
        if prime[p]:
            print(p)
 
 
# Driver code
if __name__ == '__main__':
    n = 20
    print("Following are the prime numbers smaller"),
    print("than or equal to", n)
    SieveOfEratosthenes(n)
Comment

PREVIOUS NEXT
Code Example
Rust :: rust currying, preset some arguments 
Rust :: armanriazi•rust•borrowchecker•lifetime•struct 
Rust :: armanriazi•rust•nestedtypes 
Rust :: rust•armanriazi•slice•vs•char•vec 
Rust :: armanriazi•rust•error•[E0072]: recursive type `List` has infinite size -- src/main.rs:3:1 | 3 | enum List { | ^^^^^^^^^ recursive type has infinite size 
Rust :: rust calculate every root 
Rust :: armanriazi•rust•error•already borrowed: BorrowMutError 
Rust :: rust vec length 
Lua :: roblox studio teleport on touch 
Lua :: roblox get player from character 
Lua :: lua click detection 
Lua :: how to print hello in lua 
Lua :: roblox interpolate color 
Lua :: lua loop 
Lua :: loop true childs roblox 
Lua :: string to int lua 
Lua :: what does local mean in roblox 
Lua :: how to print in lua 
Lua :: lua f animation 
Lua :: delete part on touch roblox 
Lua :: run a function in lua 
Lua :: roblox create part script 
Lua :: lua compare time 
Matlab :: matlab get row from matrix 
Matlab :: matlab symbolic simplify fraction 
Matlab :: matlab text subscript 
Basic :: online c++ to c converter 
Elixir :: elixir ecto query to sql 
Elixir :: elixir function arity 
Scala :: scala remove element from list 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =