Skip to content

Instantly share code, notes, and snippets.

# tkmharris/hurwitz_radon.py

Last active October 13, 2018 21:13
Show Gist options
• Save tkmharris/6317809e1998020fe8a5636df0e6b76e to your computer and use it in GitHub Desktop.
Calculate the Hurwitz-Radon number of a positive integer
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
 def hurwitz_radon(n): '''Takes a positve integer n; returns the Hurwitz-Radon number p(n). p(n) defined as follows: Let n = (2^v)m with m odd and write v = 4a + b, 0<= b < 4. Then p(n) = 8a + 2^b.''' if not isinstance(n, int): raise TypeError("hurwitz_radon only defined for positive integers") if n <= 0: raise ValueError("hurwitz_radon only defined for positive integers") # helper to get largest power of 2 divinding n pow2 = lambda x : 0 if x%2 != 0 else (1 + pow2(x/2)) v = pow2(n) a,b = divmod(v,4) return 8*a + 2**b ''' ## TESTS if hurwitz_radon(8) == 8: print "Test #1 passed" else: print "Test #1 failed" if hurwitz_radon(9920) == 12: print "Test #2 passed" else: print "Test #2 failed" if hurwitz_radon(1671) == 1: print "Test #3 passed" else: print "Test #3 failed" try: hurwitz_radon(-5) except ValueError: print "Test #4 passed" except Exception: print "Test #4 failed" else: print "Test #4 failed" try: hurwitz_radon(1.0) except TypeError: print "Test #5 passed" except Exception: print "Test #5 failed" else: print "Test #5 failed" try: hurwitz_radon("Blistering barnacles") except TypeError: print "Test #6 passed" except Exception: print "Test #6 failed" else: print "Test #6 failed" '''
to join this conversation on GitHub. Already have an account? Sign in to comment