Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
RQuispeC wants to merge6 commits intoabranhe:masterfromRQuispeC:master
Closed
Show file tree
Hide file tree
Changes fromall 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 deletionsallalgorithms/math/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
from .max_numbers import find_max
31 changes: 31 additions & 0 deletionsallalgorithms/math/prime_numbers.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionallalgorithms/numeric/__init__.py
View file
Open in desktop

This file was deleted.

34 changes: 34 additions & 0 deletionsdocs/math/prime-numbers.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/test_numeric.py → tests/test_math.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp