Population count: Difference between revisions
Content added Content deleted
(Added Run BASIC) |
(added Ol) |
||
Line 3,175: | Line 3,175: | ||
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 |
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 |
||
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 ok |
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 ok |
||
</pre> |
|||
=={{header|Ol}}== |
|||
<syntaxhighlight lang="scheme"> |
|||
(define (popcount n) |
|||
(let loop ((n n) (c 0)) |
|||
(if (= n 0) |
|||
c |
|||
(loop (>> n 1) |
|||
(if (eq? (band n 1) 0) c (+ c 1)))))) |
|||
(print (popcount 31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253)) |
|||
(define thirty 30) |
|||
(display "popcount:") |
|||
(for-each (lambda (i) |
|||
(display " ") |
|||
(display (popcount (expt 3 i)))) |
|||
(iota thirty 0)) |
|||
(print) |
|||
(define (evenodd name test) |
|||
(display name) (display ":") |
|||
(for-each (lambda (i) |
|||
(display " ") |
|||
(display i)) |
|||
(reverse |
|||
(let loop ((n 0) (i 0) (out '())) |
|||
(if (= i thirty) |
|||
out |
|||
(if (test (popcount n)) |
|||
(loop (+ n 1) (+ i 1) (cons n out)) |
|||
(loop (+ n 1) i out)))))) |
|||
(print)) |
|||
(evenodd "evil" even?) |
|||
(evenodd "odius" odd?) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
159 |
|||
popcount: 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 |
|||
odius: 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 |
|||
</pre> |
</pre> |
||