#!/usr/bin/python from math import sqrt import string # Mathfun is released under the LGPL. Please see the included # LICENSE file for more information. def isprime (spam_num): if spam_num % 2 == 0 and not spam_num == 2: return (0) else: eggs = int(sqrt(spam_num)) + 1 for count in range (3, eggs, 2): if spam_num % count == 0: return (0) return (1) def nextprime (spam_num): # Determines the next prime number after the one given. np = spam_num + 1 while np: if IsPrime(np): return (np) np = np + 1 def prevprime (spam_num): # Determines the first prime number before the one give. pp = spam_num - 1 while pp: if IsPrime(pp): return (pp) pp = pp - 1 def primerange (bot_num, top_num, max_ret=0): # Finds all prime numbers in the range given, including the high and low numbers # If max_ret is specified, then stops number returned if number exceeds max. # Returns list. lop = [] for count in range (bot_num, top_num+1): if IsPrime(count): lop.append(count) if max_ret and len(lop) == max_ret: break return lop def fibseq (bunny_one, bunny_two = 0, count = 0): # Generates a list of numbers in a Fibonacci sequence. # Watch out, a high 'count' gets big numbers fast. if not count: count = 10 if not bunny_two: bunny_two = bunny_one rabbits = [bunny_one, bunny_two] for spam in range (count+1): rabbits.append (rabbits[-1:][0] + rabbits[-2:-1][0]) return rabbits def primefactors (spam_num): # Returns list of prime factors for given number. pf = [] while spam_num % 2 == 0: pf.append (2) spam_num = spam_num / 2 eggs = 3 while not spam_num == 1: if spam_num % eggs == 0: pf.append (eggs) spam_num = spam_num / eggs else: eggs = eggs + 2 return (pf) def numsum(a_number, down_to_one_digit = 1): # Return an int that represents the sum of all individual digits # in a number. numsum(25) would return 7 for instance. # Down_to_one_digit determines whether or not this process is # repeated for numsums that themselves contain 2 digits, like 59. # A numsum(59) would normally return 5, unless you did numsum(59,False) # which would yield 14. ret_value = 0 num_str = str(a_number) for char in num_str: ret_value = ret_value + int(char) if ((len(str(ret_value)) > 1) and (down_to_one_digit)): ret_value = numsum(ret_value) return (ret_value) def asciisum(a_string): # asciisum iterates through a string (that has had all of it's # whitespace removed) and returns an int that is the sum of all # of the ascii values of the characters within the string. # Case matters, if you're wondering... ret_value = 0 dummy_string = string.join(string.split(a_string), "") for char in dummy_string: ret_value = ret_value + ord(char) return (ret_value) def alphasum(a_string): # alphasum iterates through a string (after removing any whitespace) # and returns an int that is the sum of the 'position' of the letter # in the alphabet. A=1, B=2 and so on. Case doesn't matter. # Non-letters (!*%&@ and so on) will be ignored. letter_hash = {"a":1, "b":2, "c":3, "d":4, "e":5, "f":6, "g":7, "h":8, "i":9, "j":10, "k":11, "l":12, "m":13, "n":14, "o":15, "p":16, "q":17, "r":18, "s":19, "t":20, "u":21, "v":22, "w":23, "x":24, "y":25, "z":26} ret_value = 0 dummy_string = string.lower(string.join(string.split(a_string), "")) for char in dummy_string: if char in string.lowercase: ret_value = ret_value + letter_hash[char] return (ret_value) if __name__ == '__main__': print "Just to show you that the bug is fixed... isprime(25)!" print isprime(25) print "Quick demo of the new functionality of mathfun2" print "The numsum of my birthdate! (19750209)" print numsum(19750209) print "Is BILL GATES evil? Let's find out!" print asciisum("BILL GATES") print "663? I guess that's why he's actually Bill Gates III, no?" print "The alphasum of this sentence is..." print alphasum("The alphasum of this sentence is...")