Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- __author__ = 'Julio Tentor jtentor@gmail.com, Brian Mayo brianmay.dev@gmail.com'
- from math import sqrt
- from functools import reduce
- from time import clock
- def timeformat(ticks):
- hours, rem = divmod(ticks, 3600)
- minutes, seconds = divmod(rem, 60)
- return "{:0>2}:{:0>2}:{:09.6f}".format(int(hours), int(minutes), seconds)
- def isprime(number):
- if number < 1:
- raise ValueError("Error de argumento en isprime")
- if number == 1 or number == 2:
- return True
- if number % 2 == 0:
- return False
- for divider in range(3, int(sqrt(number)) + 1, 2):
- if number % divider == 0:
- return False
- return True
- def show(number, dividers):
- print("Divisores", dividers)
- for divider in dividers:
- print("%5d" % divider, "+", "%5d" % number, "/", "%5d" % divider, "=", "%5d" % (divider + number / divider))
- def checkprimes(number, dividers):
- for divider in dividers:
- if not isprime(divider + number / divider):
- return False
- return True
- def testproceduralprogramming(limit):
- starttime = clock()
- for number in range(limit, 1, -1):
- dividers = [1]
- for divider in range(2, number):
- if number % divider == 0:
- dividers.append(divider)
- dividers.append(number)
- if checkprimes(number, dividers):
- print("El numero que buscamos es", number)
- #show(number, dividers)
- break
- stoptime = clock()
- print("Tiempo de ejecución versión Procedural", timeformat(stoptime - starttime))
- def testfunctionalprogramming(limit):
- starttime = clock()
- for number in range(limit, 1, -1):
- dividers = [1] + [divider for divider in range(2, number) if number % divider == 0] + [number]
- if reduce(lambda a, b: a and b, map(lambda divider: isprime(divider + number / divider), dividers)):
- print("El numero que buscamos es", number)
- #show(number, dividers)
- break
- stoptime = clock()
- print("Tiempo de ejecución versión Funcional ", timeformat(stoptime - starttime))
- def test(limit):
- testproceduralprogramming(limit)
- testfunctionalprogramming(limit)
- if __name__ == '__main__': test(10000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement