RosettaCodeData/Task/Population-count/Oforth/population-count.fth

13 lines
365 B
Forth
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
: popcount(n)
0 while ( n ) [ n isOdd + n bitRight(1) ->n ] ;
: test
| i count |
30 seq map(#[ 3 swap 1- pow ]) map(#popcount) println
0 ->count
0 while( count 30 <> ) [ dup popcount isEven ifTrue: [ dup . count 1+ ->count ] 1+ ] drop printcr
0 ->count
0 while( count 30 <> ) [ dup popcount isOdd ifTrue: [ dup . count 1+ ->count ] 1+ ] drop ;