Skip to content

Added algorithm to compute prime numbers less than x #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions allalgorithms/math/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .max_numbers import find_max
31 changes: 31 additions & 0 deletions allalgorithms/math/prime_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: UTF-8 -*-
#
# Numeric Algorithms
# The All ▲lgorithms library for python
#
# Contributed by: Rodolfo
# Github: @RQuispeC
#
import math

SIEVE_LIMIT = 1000000

def sieve(n):
prime = [True]*(n+1)
prime[0] = False
prime[1] = False
for i in range(2,int(math.sqrt(n)) + 1):
if prime[i]:
for j in range(i*i, n+1, i):
prime[j] = False
return prime

def prime_lower(n):
if n < 1 or n > SIEVE_LIMIT:
return []
prime = sieve(n)
ans = []
for i in range(n+1):
if prime[i]:
ans.append(i)
return ans
1 change: 0 additions & 1 deletion allalgorithms/numeric/__init__.py

This file was deleted.

34 changes: 34 additions & 0 deletions docs/math/prime-numbers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Prime Numbers

Using prime numbers is common in many problems. We implement various algorithms using them.

## Install

```
pip install allalgorithms
```

## Usage

```py
from allalgorithms.math import prime_lower

print(prime_lower(18))
# -> [2, 3, 5, 7, 11, 13, 17]

print(prime_lower(-1))
# -> []

print(prime_lower(1000001))
# -> []
```

## API

### prime_lower(query)

> Return array of prime numbers lower than `query`, `query` must be lower than `10e6` to avoid memory problems.

##### Params:

- `query`: superior limit for the array of prime number
5 changes: 3 additions & 2 deletions tests/test_numeric.py → tests/test_math.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import unittest

from allalgorithms.numeric import find_max

from allalgorithms.math import (
find_max
)

class TestMax(unittest.TestCase):

Expand Down