Skip to content

Instantly share code, notes, and snippets.

View salma-rodriguez's full-sized avatar

Salma Rodriguez salma-rodriguez

View GitHub Profile
@salma-rodriguez
salma-rodriguez / sieve.jl
Created December 17, 2018 22:41 — forked from narenaryan/sieve.jl
A Sieve of Eratosthenes implementation in Julia
# Sieve of Eratosthenes, docstrings coming in Julia 0.4
function es(n::Int64) # accepts one 64 bit integer argument
isprime = ones(Bool, n) # n-element vector of true-s
isprime[1] = false # 1 is not a prime
for i in 2:Int64(round(sqrt(n))) # loop integers from 2 to sqrt(n), explicit conversion to integer
if isprime[i] # conditional evaluation
for j in (i*i):i:n # sequence from i^2 to n with step i
isprime[j] = false # j is divisible by i
end
end
@seankross
seankross / Sieve of Eratosthenes.R
Last active October 20, 2022 17:09
Counting primes using the Sieve of Eratosthenes in R
# The Sieve of Eratosthenes
# Given a number greater than zero this function will return a list of primes between 2 and the number given as argument.
sieveOfEratosthenes <- function(num){
values <- rep(TRUE, num)
values[1] <- FALSE
prev.prime <- 2
for(i in prev.prime:sqrt(num)){
values[seq.int(2 * prev.prime, num, prev.prime)] <- FALSE
prev.prime <- prev.prime + min(which(values[(prev.prime + 1) : num]))