Skip to content

Instantly share code, notes, and snippets.

@ashgillman
Last active April 24, 2017 22:35
Show Gist options
  • Save ashgillman/e84073d5cac0ece3e41c97d05483d159 to your computer and use it in GitHub Desktop.
Save ashgillman/e84073d5cac0ece3e41c97d05483d159 to your computer and use it in GitHub Desktop.
Euler Project
def sieve_up_to(n):
possible = list(range(2, n+1))
primes = []
for i, p in enumerate(possible):
if p:
primes.append(p)
for j in range(i, n-1, p):
possible[j] = False
return primes
from math import ceil
from functools import reduce
from operator import add
def factors(num):
sqrt_num = ceil(num**0.5)
facts = reduce(add, ([f, num//f] if num % f == 0 else []
for f in range(1, sqrt_num)),
[])
if sqrt_num*sqrt_num == num:
facts.append(sqrt_num)
return facts
def n_factors(num):
n_fact = 1
for f in primes:
this_n_prime_f = 0
if num == 1: break
while num % f == 0:
#print('f', f)
num //= f
this_n_prime_f += 1
n_fact *= this_n_prime_f + 1
return n_fact
from bisect import bisect_left
def sorted_contains(l, list_):
i = bisect_left(list_, l)
return i != len(list_) and list_[i] == l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment