Attractive numbers: Difference between revisions
Content added Content deleted
m (→{{header|JavaScript}}: one renamed function) |
(added Tcl) |
||
Line 2,511: | Line 2,511: | ||
<pre>Attractive numbers up to and including 120: [4, 6, 8, 9, 10, 12, 14, 15, 18, 20, 21, 22, 25, 26, 27, 28, 30, 32, 33, 34, 35, 38, 39, 42, 44, 45, 46, 48, 49, 50, 51, 52, 55, 57, 58, 62, 63, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 82, 85, 86, 87, 91, 92, 93, 94, 95, 98, 99, 102, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119, 120]</pre> |
<pre>Attractive numbers up to and including 120: [4, 6, 8, 9, 10, 12, 14, 15, 18, 20, 21, 22, 25, 26, 27, 28, 30, 32, 33, 34, 35, 38, 39, 42, 44, 45, 46, 48, 49, 50, 51, 52, 55, 57, 58, 62, 63, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 82, 85, 86, 87, 91, 92, 93, 94, 95, 98, 99, 102, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119, 120]</pre> |
||
=={{header|Tcl}}== |
|||
<lang Tcl>proc isPrime {n} { |
|||
if {$n < 2} { |
|||
return 0 |
|||
} |
|||
if {$n > 3} { |
|||
if {0 == ($n % 2)} { |
|||
return 0 |
|||
} |
|||
for {set d 3} {($d * $d) <= $n} {incr d 2} { |
|||
if {0 == ($n % $d)} { |
|||
return 0 |
|||
} |
|||
} |
|||
} |
|||
return 1 ;# no divisor found |
|||
} |
|||
proc cntPF {n} { |
|||
set cnt 0 |
|||
while {0 == ($n % 2)} { |
|||
set n [expr {$n / 2}] |
|||
incr cnt |
|||
} |
|||
for {set d 3} {($d * $d) <= $n} {incr d 2} { |
|||
while {0 == ($n % $d)} { |
|||
set n [expr {$n / $d}] |
|||
incr cnt |
|||
} |
|||
} |
|||
if {$n > 1} { |
|||
incr cnt |
|||
} |
|||
return $cnt |
|||
} |
|||
proc showRange {lo hi} { |
|||
puts "Attractive numbers in range $lo..$hi are:" |
|||
set k 0 |
|||
for {set n $lo} {$n <= $hi} {incr n} { |
|||
if {[isPrime [cntPF $n]]} { |
|||
puts -nonewline " [format %3s $n]" |
|||
incr k |
|||
} |
|||
if {$k >= 20} { |
|||
puts "" |
|||
set k 0 |
|||
} |
|||
} |
|||
if {$k > 0} { |
|||
puts "" |
|||
} |
|||
} |
|||
showRange 1 120</lang> |
|||
{{out}} |
|||
<pre>Attractive numbers in range 1..120 are: |
|||
4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32 33 34 |
|||
35 38 39 42 44 45 46 48 49 50 51 52 55 57 58 62 63 65 66 68 |
|||
69 70 72 74 75 76 77 78 80 82 85 86 87 91 92 93 94 95 98 99 |
|||
102 105 106 108 110 111 112 114 115 116 117 118 119 120 |
|||
</pre> |
|||
=={{header|Vala}}== |
=={{header|Vala}}== |