Created
May 10, 2012 04:40
-
-
Save pygeek/2651065 to your computer and use it in GitHub Desktop.
Outputs divisors for a specified range of numbers. Prints 'PRIME' if number is prime.
This file contains 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 | |
import timeit | |
class Divisors(): | |
""" Outputs dividends a specified range of divisors. | |
Prints 'PRIME' if number is prime. | |
Outputs other interesting data. | |
""" | |
def __init__(self): | |
upper_bounds = self.upper_bounds() | |
divisors = range(1,upper_bounds+1) | |
dividends = range(1,upper_bounds + 1) | |
calc = self.calc_and_output(divisors,dividends) | |
self.conclusion(calc[0],calc[1]) | |
def upper_bounds(self): | |
try: | |
upper_bounds = int(raw_input('Please specify the upper bounds: ')) | |
except: | |
print('Sorry, upper bounds must be an integer between 1 and infinity.') | |
return self.upper_bounds() | |
return upper_bounds | |
def calc_and_output(self,divisors,dividends): | |
primes = [] | |
longest_divisor_dividends = [[]] | |
for divisor in divisors: | |
divisor_dividends = [] | |
for dividend in dividends: | |
if dividend > divisor: | |
break; | |
if divisor % dividend == 0: | |
divisor_dividends.append(dividend) | |
if len(divisor_dividends) == 2: | |
primes.append(divisor) | |
print('{0}: {1} PRIME {1}').format(divisor,'-'*15) | |
else: | |
print('{0}: {1}').format(divisor,divisor_dividends) | |
if len(longest_divisor_dividends[0]) < len(divisor_dividends): | |
longest_divisor_dividends = [] | |
longest_divisor_dividends.append(divisor_dividends) | |
if len(longest_divisor_dividends[0]) == len(divisor_dividends): | |
longest_divisor_dividends.append(divisor_dividends) | |
return [primes,longest_divisor_dividends] | |
def conclusion(self,primes,longest_divisor_dividends): | |
print('\n{0}').format('-'*80) | |
print('There were {0} prime numbers in this sequence.').format(len(primes)) | |
print('Prime numbers in this sequence: {0}').format(primes) | |
integers = sorted(set([divisors[-1] for divisors in longest_divisor_dividends])) | |
print('{0} had {1} divisors, which is the most found for any one integer in this in this sequence.').format(integers,len(longest_divisor_dividends[0])) | |
Divisors() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This revision returns correct results. :-]