Skip to content

Instantly share code, notes, and snippets.

@danilobellini
Last active July 25, 2016 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilobellini/7233352 to your computer and use it in GitHub Desktop.
Save danilobellini/7233352 to your computer and use it in GitHub Desktop.
Find all the divisors from a given number
#!/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()))
#!/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