Vampire number: Difference between revisions

(→‎{{header|Wren}}: Now uses new core library method.)
Line 2,026:
16758243290880 : [1982736, 8452080][2123856, 7890480][2751840, 6089832][2817360, 5948208]
24959017348650 : [2947050, 8469153][2949705, 8461530][4125870, 6049395][4129587, 6043950][4230765, 5899410]</pre>
 
=={{header|jq}}==
{{trans|Ruby}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
<lang jq>def count(s): reduce s as $x (0; .+1);
 
# To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
# Output: a possibly empty array of pairs
def factor_pairs:
. as $n
| ($n / (10 | power($n|tostring|length / 2) - 1)) as $first
| [range($first|floor; 1 + ($n | sqrt)) | select(($n % .) == 0) | [., $n / .] ] ;
 
# Output: a stream
def vampire_factors:
def es: tostring|explode;
. as $n
| tostring as $s
| ($s|length) as $nlen
| if ($nlen % 2) == 1 then []
else ($nlen / 2 ) as $half
| [factor_pairs[]
| select(. as [$a, $b]
| ($a|tostring|length) == $half and ($b|tostring|length) == $half
and count($a, $b | select(.%10 == 0)) != 2
and ((($a|es) + ($b|es)) | sort) == ($n|es|sort) ) ]
end ;
 
def task1($n):
{i:0,
vamps: 0 }
| while(.vamps <= $n;
.emit = null
| .i += 1
| (.i|vampire_factors) as $vf
| if $vf == [] then .
else .emit = "\(.i):\t\($vf)"
| .vamps += 1
end)
| select(.emit).emit ;
 
def task2:
16758243290880, 24959017348650, 14593825548650
| vampire_factors as $vf
| if $vf|length == 0
then "\(.) is not a vampire number!"
else "\(.):\t\($vf)"
end;
 
task1(25),
"",
task2</lang>
{{out}}
<pre>
1260: [[21,60]]
1395: [[15,93]]
1435: [[35,41]]
1530: [[30,51]]
1827: [[21,87]]
2187: [[27,81]]
6880: [[80,86]]
102510: [[201,510]]
104260: [[260,401]]
105210: [[210,501]]
105264: [[204,516]]
105750: [[150,705]]
108135: [[135,801]]
110758: [[158,701]]
115672: [[152,761]]
116725: [[161,725]]
117067: [[167,701]]
118440: [[141,840]]
120600: [[201,600]]
123354: [[231,534]]
124483: [[281,443]]
125248: [[152,824]]
125433: [[231,543]]
125460: [[204,615],[246,510]]
125500: [[251,500]]
 
16758243290880: [[1982736,8452080],[2123856,7890480],[2751840,6089832],[2817360,5948208]]
24959017348650: [[2947050,8469153],[2949705,8461530],[4125870,6049395],[4129587,6043950],[4230765,5899410]]
14593825548650 is not a vampire number!
</pre>
 
 
=={{header|Julia}}==
2,484

edits