Attractive numbers: Difference between revisions

Insitux implementation
(Insitux implementation)
Line 1,705:
{{Out}}
<pre>[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|Insitux}}==
Notice that this implementation is not optimally permonant, as primes is called multiple times when the output could be shared, the same is true for distinct-factor and factor.
<syntaxhighlight lang="insitux">
(function primes n
(let find-range (range 2 (inc n))
check-nums (range 2 (-> n ceil sqrt inc))
skip-each-after #(skip-each % (skip %1 %2))
muls (xmap #(drop 0 (skip-each-after (dec %1) % find-range)) check-nums))
(remove (flatten muls) find-range))
(function distinct-factor n
(filter @(div? n) (primes n)))
(function factor n
(map (fn t (find (div? n) (map @(** t) (range (round (sqrt n)) 0)))) (distinct-factor n)))
(function decomposed-factors n
(map (fn dist t (repeat dist (/ (logn t) (logn dist)))) (distinct-factor n) (factor n)))
(var prime? @((primes %)))
 
(var attract-num? (comp decomposed-factors flatten len prime?))
(filter attract-num? (range 121))</syntaxhighlight>
 
=={{header|J}}==
5

edits