Last active
July 25, 2016 12:46
-
-
Save danilobellini/7233352 to your computer and use it in GitHub Desktop.
Find all the divisors from a given number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Created on Wed Oct 30 10:55:51 2013 | |
# By Danilo J. S. Bellini | |
# Based on https://gist.github.com/anonymous/7123189 | |
""" | |
Find all the divisors from a given number | |
""" | |
# Ensure compatibility in both Python 2 and 3 | |
from __future__ import division, print_function, unicode_literals | |
import sys | |
if sys.version_info.major == 2: | |
input = raw_input | |
INT_TYPES = (int, getattr(sys.modules["__builtin__"], "long", None)) | |
else: # Python 3 | |
INT_TYPES = (int,) | |
from itertools import count, takewhile | |
def prime_gen(): | |
""" Prime number generator """ | |
yield 2 | |
primes = [] # From now on, there's only odd numbers to care about | |
for value in count(start=3, step=2): | |
iter_primes = takewhile(lambda x: x * x <= value, primes) | |
if all(value % p != 0 for p in iter_primes): | |
primes.append(value) | |
yield value | |
def divisors(n): | |
""" Sorted divisors generator for a given number besides 1, with repeats """ | |
if not isinstance(n, INT_TYPES): | |
raise TypeError("Input have to be an integer.") | |
if n <= 0: | |
raise ValueError("Given input isn't positive.") | |
remain = n | |
for p in prime_gen(): | |
if remain == 1: | |
break | |
while remain % p == 0: | |
remain //= p | |
yield p | |
if __name__ == "__main__": | |
n = int(input("Type in a positive integer number: ")) | |
size = len(str(n)) | |
for d in divisors(n): | |
print("{n:{size}} | {d}".format(**locals())) | |
n //= d | |
print("{n:{size}} | -".format(**locals())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Created on Tue Dec 03 16:27:04 2013 | |
# By Danilo J. S. Bellini | |
""" | |
Prime number generation duration comparison | |
""" | |
from __future__ import print_function, unicode_literals | |
from itertools import count, takewhile | |
from collections import deque | |
from time import time | |
def prime_gen_list(): | |
""" Prime number generator using a list for internal storage """ | |
yield 2 | |
primes = [] # From now on, there's only odd numbers to care about | |
for value in count(start=3, step=2): | |
iter_primes = takewhile(lambda x: x * x <= value, primes) | |
if all(value % p != 0 for p in iter_primes): | |
primes.append(value) | |
yield value | |
def prime_gen_deque(): | |
""" Prime number generator using a deque for internal storage """ | |
yield 2 | |
primes = deque() | |
for value in count(start=3, step=2): | |
iter_primes = takewhile(lambda x: x * x <= value, primes) | |
if all(value % p != 0 for p in iter_primes): | |
primes.append(value) | |
yield value | |
duration = 10 * 60 # seconds | |
start = time() | |
for p in prime_gen_list(): | |
if time() - start > duration: | |
break | |
prime_list = p | |
start = time() | |
for p in prime_gen_deque(): | |
if time() - start > duration: | |
break | |
prime_deque = p | |
print("Duration (for each generator):", duration, "seconds") | |
print("Prime found with a list for storage:", prime_list) | |
print("Prime found with a deque for storage:", prime_deque) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment