added ability to specify starting number of digits for collatz search, and a quick and dirty test of a power of 2 algorithm.
This commit is contained in:
parent
054879c11d
commit
828aaaee0a
|
@ -3,7 +3,9 @@
|
||||||
import sys
|
import sys
|
||||||
import collatz
|
import collatz
|
||||||
|
|
||||||
usage='usage: collatzSearch [digits] outputs the lenght of the collatz sequence for every number less than 10^digits'
|
usage='usage: collatzSearch [digits] ([startDigits]) outputs the lenght of the collatz sequence for every number less than 10^digits, starting with 10^startDidgits.'
|
||||||
|
|
||||||
|
nextNumber = 1
|
||||||
|
|
||||||
if len(sys.argv)<2:
|
if len(sys.argv)<2:
|
||||||
print(usage)
|
print(usage)
|
||||||
|
@ -15,8 +17,13 @@ try:
|
||||||
except:
|
except:
|
||||||
print(usage)
|
print(usage)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
if len(sys.argv)>=3:
|
||||||
|
try:
|
||||||
|
nextNumber = 10**int(sys.argv[2])
|
||||||
|
except:
|
||||||
|
print(usage)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
nextNumber = 1
|
|
||||||
maxNumber = 10**digits
|
maxNumber = 10**digits
|
||||||
|
|
||||||
while nextNumber<maxNumber:
|
while nextNumber<maxNumber:
|
||||||
|
|
|
@ -15,7 +15,9 @@ try:
|
||||||
except:
|
except:
|
||||||
print(usage)
|
print(usage)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
seqLength = 0
|
||||||
while number != 1:
|
while number != 1:
|
||||||
number = collatz.nextInSequence(number)
|
number = collatz.nextInSequence(number)
|
||||||
|
seqLength += 1
|
||||||
print(number)
|
print(number)
|
||||||
|
print('length of '+str(seqLength))
|
||||||
|
|
17
powerOf2.py
Executable file
17
powerOf2.py
Executable file
|
@ -0,0 +1,17 @@
|
||||||
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
|
def slowMethod(n):
|
||||||
|
while (n!=1):
|
||||||
|
if (n%2!=0):
|
||||||
|
return False
|
||||||
|
n = n //2
|
||||||
|
return True
|
||||||
|
|
||||||
|
def fastMethod(n):
|
||||||
|
return (n&(n-1))==0
|
||||||
|
|
||||||
|
for n in range(1,1000000):
|
||||||
|
if slowMethod(n):
|
||||||
|
print(n)
|
||||||
|
if fastMethod(n):
|
||||||
|
print(n)
|
Loading…
Reference in a new issue