|
Post by tenochtitlanuk on Oct 10, 2019 13:05:51 GMT -5
Was at a loose end with half an hour to kill. Had a quick look on Rosetta code for a new task, and found an interesting one- binary population count. The population count is the number of 1s (ones) in the binary representation of a non-negative integer. For example, 5 (which is 101 in binary) has a population count of 2.
Evil numbers are non-negative integers that have an even population count.
Odious numbers are positive integers that have an odd population count You should get the results quoted there- my code prints ai a more illustrative way. Will put the code up after ( perhaps) others have a go. HINT decimal to binary routines are already on Rosetta Code.
|
|
|
Post by tsh73 on Oct 10, 2019 23:29:04 GMT -5
I can't quite get why number with even count of 1's considered evil, but I like binary plot from linked page EDIT I wonder if some interesting image hides in your 0's and 1's as well. EDIT2 I wonder if any a^x (or may be INT(a^x) if a say Euler number or SQR(2) will be same neatly aligned in binary as 3^x?
|
|
|
Post by gidiom2 on Oct 11, 2019 14:15:46 GMT -5
'LB451 and Wine 11/10/2019 for x=1 to 30 print "3^"+str$(x)+tab(10); y=3^x print y;"="; b$=dec2bin$(y) l=len(b$) print tab(80-l);b$; count=0 for z=1 to l if mid$(b$,z,1)="1" then count=count+1 next z print tab(85);count; print tab(93); if count mod 2=0 then print "Evil" else print "Odious" next x end
function dec2bin$(n) while n>=1 s$=str$(n mod 2)+s$ n=int(n/2) wend dec2bin$=s$ end function
My attempt to reproduce John's output! (An interesting exercise...)
|
|
|
Post by tsh73 on Oct 11, 2019 15:20:21 GMT -5
RC-required output.
'[RC] Population count
print "3^i", for i = 0 to 29 print using("###", popCount(3^i) ); next print
print "evil", i=0: k=0 while k <30 if popCount(i) mod 2 = 0 then k=k+1 print using("###", i ); end if i=i+1 wend print
print "odious", i=0: k=0 while k <30 if popCount(i) mod 2 = 1 then k=k+1 print using("###", i ); end if i=i+1 wend print
function bin$(n) while n bin$=(n mod 2);bin$ n=int(n/2) wend if bin$="" then bin$="0" end function
function countOnes(b$) for i = 1 to len(b$) countOnes=countOnes+val(mid$(b$,i,1)) next end function
function popCount(n) popCount=countOnes(bin$(n)) end function
3^i 1 2 2 4 3 6 6 5 6 8 9 13 10 11 14 15 11 14 14 17 17 20 19 22 16 18 24 30 25 25 evil 0 3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48 51 53 54 57 58 odious 1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35 37 38 41 42 44 47 49 50 52 55 56 59
|
|
|
Post by tenochtitlanuk on Oct 11, 2019 17:19:54 GMT -5
Feel free to post it to Rosetta, Anatoly. Well programmed gidiom2 For fun, some other functions printed in binary... would be interested in any others that anyone tries... I too tend to imagine images in the random noise...
|
|