CoCalc Shared Filesdiscrete.sagews
Author: Timo Valeri Junolainen
Views : 34
Description: discrete logarithm, bruteforce
def bruteforce(s):
x=s
res='3'
buf=''
while not x==1:
if x%2==1:
x=3*x+1
buf='+'+buf
else:
x=x/2
buf='-'+buf
for a in buf:
if a=='-':
res='('+res+')*2'
else:
res='(('+res+'-1)/3)'
print buf
return res
print '35=',bruteforce(5)
print '25=',bruteforce(36)

35= ----+ ((((((3)*2)*2)*2)*2-1)/3) 25= ----+---+--+-+-+--+-- (((((((((((((((((((((((((((3)*2)*2)*2)*2-1)/3))*2)*2)*2-1)/3))*2)*2-1)/3))*2-1)/3))*2-1)/3))*2)*2-1)/3))*2)*2

x=67
while not x==1:
if x%2==1:
print "3",x,"+1=",bin(3*x+1)
x=3*x+1
else:
print x,"/2=",x/2
x=x/2

3 67 +1= 0b11001010 202 /2= 101 3 101 +1= 0b100110000 304 /2= 152 152 /2= 76 76 /2= 38 38 /2= 19 3 19 +1= 0b111010 58 /2= 29 3 29 +1= 0b1011000 88 /2= 44 44 /2= 22 22 /2= 11 3 11 +1= 0b100010 34 /2= 17 3 17 +1= 0b110100 52 /2= 26 26 /2= 13 3 13 +1= 0b101000 40 /2= 20 20 /2= 10 10 /2= 5 3 5 +1= 0b10000 16 /2= 8 8 /2= 4 4 /2= 2 2 /2= 1
for i in range(1,100):
if (i-1)%3==0 and i%2==1:
print i

1 7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97


n=6
lg=0
for i in range(1,n):
lg=lg+log(i)
print N(lg)*n

28.7249504566923