Vampire number: Difference between revisions

added Factor
(→‎{{header|Perl}}: added a faster Perl version, using the "ntheory" library)
(added Factor)
Line 1,176:
14593825548650 is not a vampire number!
</pre>
 
=={{header|Factor}}==
<lang factor>USING: combinators.short-circuit fry io kernel lists lists.lazy math
math.combinatorics math.functions math.primes.factors math.statistics
math.text.utils prettyprint sequences sets ;
IN: rosetta-code.vampire-number
 
: digits ( n -- m )
log10 floor >integer 1 + ;
 
: same-digits? ( n n1 n2 -- ? )
[ 1 digit-groups ] tri@ append [ histogram ] bi@ = ;
 
: half-len-factors ( n -- seq )
[ divisors ] [ digits ] bi 2/ '[ digits _ = ] filter ;
: same-digit-factors ( n -- seq )
dup half-len-factors 2 <combinations> [ first2 same-digits? ] with filter ;
: under-two-trailing-zeros? ( seq -- ? )
[ 10 mod ] map [ 0 = ] count 2 < ;
: tentative-fangs ( n -- seq )
same-digit-factors [ under-two-trailing-zeros? ] filter ;
: fangs ( n -- seq )
[ tentative-fangs ] [ ] bi '[ product _ = ] filter ;
: vampire? ( n -- ? )
{ [ digits even? ] [ fangs empty? not ] } 1&& ;
: first25 ( -- seq )
25 0 lfrom [ vampire? ] lfilter ltake list>array ;
: .vamp-with-fangs ( n -- )
[ pprint bl ] [ fangs [ pprint bl ] each ] bi nl ;
: part1 ( -- )
first25 [ .vamp-with-fangs ] each ;
: part2 ( -- ) { 16758243290880 24959017348650 14593825548650 }
[ dup vampire? [ .vamp-with-fangs ] [ drop ] if ] each ;
: main ( -- ) part1 part2 ;
 
MAIN: main</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 }
</pre>
 
=={{header|FreeBASIC}}==
<lang FreeBASIC>'Vampire numbers.
1,808

edits