Skip to content

1b0325h/ac-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 

Repository files navigation

Python (3.8.2)

Algorithm

Array

An imos method can be implemented as the result of repeating the operation "add a certain number v to a certain continuous interval" K times in a 1-dimensional array of length N.

Searching

A binary search is an algorithm to find a particular element in the list. Suppose we have a list of thousand elements, and we need to get an index position of a particular element. We can find the element's index position very fast using the binary search algorithm.

>>> arr = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
>>> target = 64
>>> binary_search(-1, len(arr), lambda x: arr[x] >= target)
6

Math

Number Theoretic Functions

Computes nonnegative integer greatest common divisor.

>>> igcd(8, 9)
1
>>> igcd(30, 12)
6

Computes integer least common multiple.

>>> ilcm(2, 3)
6
>>> ilcm(123, 456)
18696

Congruences

Chinese Remainder Theorem.

>>> crt([783, 278], [151, 57])
(31471, 217674)
>>> crt([12, 6, 17], [3, 4, 2])
(-1, 0)

Prime Numbers

Return a sorted list of n's prime factors.

>>> primefactors(42)
[2, 3, 7]
>>> primefactors(72)
[2, 2, 2, 3, 3]

Test if n is a prime number (True) or not (False).

>>> isprime(1)
False
>>> isprime(73)
True
>>> isprime(-1)
False

Divisors

Return all divisors of n sorted from 1..n.

>>> divisors(4)
[1, 2, 4]
>>> divisors(12)
[1, 2, 3, 4, 6, 12]

Permutations

An inverse permutation is a permutation in which each number and the number of the place which it occupies are exchanged.

>>> rperm([2, 3, 1])
[3, 1, 2]
>>> rperm([5, 3, 2, 4, 1])
[5, 3, 2, 4, 1]

Radix Representation

Converts a string representation of a number in a given base into a decimal number.

>>> decimal("123", 8)
83
>>> decimal("1234", 10)
1234

Converts a number into a string representation with the given base (radix).

>>> base(11, 2)
'1011'
>>> base(1010101, 10)
'1010101'

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Languages