Skip to content
Open

Cw #7

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
13 changes: 13 additions & 0 deletions .idea/WD2020.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ghfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
a = u"Helloł "
print(a)
a, b = 333, 56644.55
print(a)
a = 333 * 1.0 #fdfgdfg
print(a, b)
a = a/2
print(a)
a /= 2
print(a)
a = u"Helloł %(a)d" %{'a': b}
print(a)
lista = ['fdfs', "bvb", 233, 33.2]
print(lista)
slownik = {'a': b}
print(slownik)
2 changes: 2 additions & 0 deletions venv/Lib/site-packages/easy-install.pth
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
./setuptools-39.1.0-py3.6.egg
./pip-10.0.1-py3.6.egg
1 change: 1 addition & 0 deletions venv/Lib/site-packages/mathlib-0.1.3.dist-info/INSTALLER
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pip
28 changes: 28 additions & 0 deletions venv/Lib/site-packages/mathlib-0.1.3.dist-info/METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Metadata-Version: 2.1
Name: mathlib
Version: 0.1.3
Summary: A pure-python maths library
Home-page: https://github.com/spapanik/mathlib
Keywords: mathematics
Author: spapanik
Author-email: spapanik21@gmail.com
Requires-Python: >=3.6,<4.0
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Topic :: Scientific/Engineering :: Mathematics
Project-URL: Repository, https://github.com/spapanik/mathlib
Description-Content-Type: text/markdown

<p align="center">
<a href="https://github.com/ambv/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
</p>

A pure python mathematics library

10 changes: 10 additions & 0 deletions venv/Lib/site-packages/mathlib-0.1.3.dist-info/RECORD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mathlib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
mathlib/numbers.py,sha256=-9fEMvH9_w-TVCKJK22IQbhl93NGH8xgH5_NQrFMBDI,1948
mathlib/primes.py,sha256=DIbApHOomFBOqmEE3mIafLYg8vWXA1SbKy9LT978XyQ,3836
mathlib-0.1.3.dist-info/WHEEL,sha256=7rgPGIJCk4vUc5grLXztusALWdGCgLy7UJ52dff4Pnc,85
mathlib-0.1.3.dist-info/METADATA,sha256=w2TXfAYXW5eSnYRnpejsJ-pLqcyoYWpiyzYrvJSAiaI,1062
mathlib-0.1.3.dist-info/RECORD,,
mathlib-0.1.3.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
mathlib/__pycache__/numbers.cpython-36.pyc,,
mathlib/__pycache__/primes.cpython-36.pyc,,
mathlib/__pycache__/__init__.cpython-36.pyc,,
4 changes: 4 additions & 0 deletions venv/Lib/site-packages/mathlib-0.1.3.dist-info/WHEEL
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Wheel-Version: 1.0
Generator: poetry 0.12.14
Root-Is-Purelib: true
Tag: py3-none-any
Empty file.
86 changes: 86 additions & 0 deletions venv/Lib/site-packages/mathlib/numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import math
from typing import Iterator


def lcm(a: int, b: int) -> int:
"""
Find the least common multiple of a and b.

The least common multiple of a and 0 is always 0, as this
coincides with lcm to be the least upper bound in the
lattice of divisibility.
"""
return a * b // math.gcd(a, b)


def modular_inverse(n: int, mod: int) -> int:
"""
Find the modular inverse of n modulo mod.

The algorithm is based on using the Extended Euclid Algorithm,
to solve the diophantine equation n*x + mod*y = 1.
"""
original_modulo = mod
x = 1
y = 0

while n > 1:
x, y = y, x - (n // mod) * y
n, mod = mod, n % mod

if x < 0:
x += original_modulo

return x


def fibonacci(n: int, a: int = 1, b: int = 1) -> int:
"""
Return the nth Fibonacci number.

n can be a negative integer as well.
"""
values = {0: a, 1: b}

def fib(m):
if m in values:
return values[m]

k = m // 2
if m & 1 == 1:
value = fib(k) * (fib(k + 1) + fib(k - 1))
else:
value = fib(k) * fib(k) + fib(k - 1) * fib(k - 1)
values[m] = value
return value

if n < 0:
return ((-1) ** (1 - n)) * fib(-n)
return fib(n)


def fibonacci_numbers(a: int = 0, b: int = 1) -> Iterator[int]:
"""
Make an iterator that returns the Fibonacci numbers.

The Fibonacci sequence is configurable, in the sense that the two
initial values of it can be passed as arguments.
"""
while True:
yield a
a, b = b, a + b


def binomial(n: int, k: int) -> int:
"""
Calculate n choose k.

Calculation is using the multiplicative formula, and is performed
from the side that will minimise the number of calculations.
"""
output = 1
k = min(k, n - k)
for t in range(k):
output = (n - t) * output // (t + 1)

return output
168 changes: 168 additions & 0 deletions venv/Lib/site-packages/mathlib/primes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import math
from itertools import chain, count
from typing import Iterator


def sieve(upper_bound: int) -> Iterator[int]:
"""
Make an iterator that returns the primes up to upper_bound

This method uses the sieve of Eratosthenes to return the
primes.
"""
if upper_bound <= 5:
if upper_bound > 2:
yield 2

if upper_bound > 3:
yield 3

return

yield 2

upper_bound = (upper_bound >> 1) - 1
indices = [True] * upper_bound
end_range = int(math.sqrt(upper_bound)) + 1
for i in range(end_range):
if indices[i]:
slice_start = 2 * i * i + 6 * i + 3
slice_step = 2 * i + 3
number_of_primes = math.ceil(
(upper_bound - slice_start) / slice_step
)
indices[slice_start::slice_step] = [False] * number_of_primes

for i in range(upper_bound):
if indices[i]:
yield 2 * i + 3


def _miller_rabin_loop(witness, mantissa, power, n):
if pow(witness, mantissa, n) == 1:
return False

for r in range(power):
if pow(witness, mantissa * (1 << r), n) + 1 == n:
return False

return True


def _miller_rabin_witnesses(n):
if n < 2047:
return (2,)

if n < 1373653:
return 2, 3

if n < 9080191:
return 31, 73

if n < 25326001:
return 2, 3, 5

if n < 4759123141:
return 2, 7, 61

if n < 1122004669633:
return 2, 13, 23, 1662803

if n < 2152302898747:
return 2, 3, 5, 7, 11

if n < 3474749660383:
return 2, 3, 5, 7, 11, 13

if n < 341550071728321:
return 2, 3, 5, 7, 11, 13, 17

if n < 3825123056546413051:
return 2, 3, 5, 7, 11, 13, 17, 19, 23

if n < 318665857834031151167461:
return 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37

if n < 3317044064679887385961981:
return 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41

return range(2, math.floor(2 * (math.log(n) ** 2)) + 1)


def is_prime(n: int) -> bool:
"""
Check if n is a prime number.

This is a deterministic primality test, but it relies on GHR. This
seems a good enough compromise. It is very fast for up to 81-bit
integers, after which it is starts slowing down, due to the fact
that we need to check for all possible Miller-Rabin witnesses.
"""
if n < 5:
return n in (2, 3)

if n % 2 == 0:
return False

mantissa, power = n - 1, 0
while mantissa & 1 == 0:
mantissa >>= 1
power += 1

for witness in _miller_rabin_witnesses(n):
if _miller_rabin_loop(witness, mantissa, power, n):
return False

return True


def next_prime(n: int) -> int:
"""
Get the smallest prime that is larger than n.
"""
if n < 2:
return 2

if n == 2:
return 3

if n & 1 == 1:
n += 2
else:
n += 1

for n in count(n, 2):
if is_prime(n):
return n


def primes() -> Iterator[int]:
"""
Make an iterator that returns the prime numbers in ascending order.
"""
yield 2

for n in count(3, 2):
if is_prime(n):
yield n


def divisor_sigma(n: int, x: int = 0) -> int:
"""
Calculate the sum of the xth powers of the positive divisors of n
"""
out = 1
for prime_div in chain([2], count(3, 2)):
power = 1
while n % prime_div == 0:
power += 1
n //= prime_div
if power != 1:
if x == 0:
out *= power
elif x == 1:
out *= (prime_div ** power - 1) // (prime_div - 1)
else:
out *= (prime_div ** (x * power) - 1) // (prime_div ** x - 1)
if n == 1:
return out
Loading