Vampire number: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(44 intermediate revisions by 21 users not shown)
Line 1:
{{task|Mathematics}}
 
A [[wp:Vampire_number|vampire number]] is a natural decimal number with an even number of digits,   that can be factored into two integers. These two factors are called the ''fangs'', and must have the following properties:
* they each contain half the number of the digits of the original number
* together they consist of exactly the same digits as the original number
* at most one of them has a trailing zero
 
These two factors are called the   ''fangs'',   and must have the following properties:
<br>
::* &nbsp; they each contain half the number of the decimal digits of the original number
An example of a Vampire number and its fangs: <code> 1260 : (21, 60) </code>
::* &nbsp; together they consist of exactly the same decimal digits as the original number
::* &nbsp; at most one of them has a trailing zero
 
 
An example of a vampire number and its fangs: &nbsp; <code> 1260 : (21, 60) </code>
 
 
;Task:
# Print the first &nbsp; '''25''' &nbsp; Vampirevampire numbers and their fangs.
# Check if the following numbers are Vampirevampire numbers and, &nbsp; if so, &nbsp; print them and their fangs:
<big><code> 16758243290880, 24959017348650, 14593825548650 </code></big>
 
 
<br>
Note that a Vampirevampire number can have more than one pair of fangs.
 
 
;See also:
* [http://www.numberphile.com/videos/vampire_numbers.html numberphile.com].
* [http://users.cybercity.dk/~dsl522332/math/vampires/ Vampirevampire search algorithm]
* [[oeis:A014575|Vampirevampire numbers on OEIS]]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">-V Pow10 = (0..18).map(n -> Int64(10) ^ n)
 
F isOdd(n)
R (n [&] 1) != 0
 
F fangs(n)
[(Int64, Int64)] r
 
V nDigits = sorted(String(n))
I isOdd(nDigits.len)
R r
 
V fangLen = nDigits.len I/ 2
V inf = Pow10[fangLen - 1]
L(d) inf .< inf * 10
I n % d != 0
L.continue
V q = n I/ d
I q < d
R r
V dDigits = String(d)
V qDigits = String(q)
I qDigits.len > fangLen
L.continue
I qDigits.len < fangLen
R r
I nDigits != sorted(dDigits‘’qDigits)
L.continue
I dDigits.last != ‘0’ | qDigits.last != ‘0’
r.append((d, q))
 
R r
 
print(‘First 25 vampire numbers with their fangs:’)
V count = 0
V n = 10
V limit = 100
L count != 25
V fangList = fangs(n)
I !fangList.empty
count++
print((‘#2: #6 =’.format(count, n))‘ ’fangList.map(it -> ‘#3 x #3’.format(it[0], it[1])).join(‘ = ’))
n++
I n == limit
n *= 10
limit *= 10
 
print()
L(n) [16'758'243'290'880, 24'959'017'348'650, 14'593'825'548'650]
V fangList = fangs(n)
I fangList.empty
print(n‘ is not vampiric.’)
E
print(n‘ = ’fangList.map(it -> ‘#. x #.’.format(it[0], it[1])).join(‘ = ’))</syntaxhighlight>
 
{{out}}
<pre>
First 25 vampire numbers with their fangs:
1: 1260 = 21 x 60
2: 1395 = 15 x 93
3: 1435 = 35 x 41
4: 1530 = 30 x 51
5: 1827 = 21 x 87
6: 2187 = 27 x 81
7: 6880 = 80 x 86
8: 102510 = 201 x 510
9: 104260 = 260 x 401
10: 105210 = 210 x 501
11: 105264 = 204 x 516
12: 105750 = 150 x 705
13: 108135 = 135 x 801
14: 110758 = 158 x 701
15: 115672 = 152 x 761
16: 116725 = 161 x 725
17: 117067 = 167 x 701
18: 118440 = 141 x 840
19: 120600 = 201 x 600
20: 123354 = 231 x 534
21: 124483 = 281 x 443
22: 125248 = 152 x 824
23: 125433 = 231 x 543
24: 125460 = 204 x 615 = 246 x 510
25: 125500 = 251 x 500
 
16758243290880 = 1982736 x 8452080 = 2123856 x 7890480 = 2751840 x 6089832 = 2817360 x 5948208
24959017348650 = 2947050 x 8469153 = 2949705 x 8461530 = 4125870 x 6049395 = 4129587 x 6043950 = 4230765 x 5899410
14593825548650 is not vampiric.
</pre>
 
=={{header|AutoHotkey}}==
The following code should work for older (1.0.*) AHK versions as well:
<langsyntaxhighlight AutoHotkeylang="autohotkey">SetBatchLines -1 ; used to improve performance
; (you can make it much faster by removing the informative tooltips)
 
Line 100 ⟶ 194:
}
Return SubStr(Output,3) ; 3 = 1 + length of "`n`t"
}</langsyntaxhighlight>
{{out}}
<pre>1260: [21,60]
Line 143 ⟶ 237:
 
14593825548650:</pre>
 
=={{header|Bracmat}}==
<langsyntaxhighlight Bracmatlang="bracmat">( ( vampire
= N len R fangsList
. !arg:@(?N:? [?len)
Line 204 ⟶ 299:
& (!count:<25|!bignums:%?i ?bignums)
)
);</langsyntaxhighlight>
Output:
<pre>1260 (21,60)
Line 244 ⟶ 339:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Line 325 ⟶ 420:
 
return 0;
}</langsyntaxhighlight>
<pre> 1: 1260 = 21 x 60
2: 1395 = 15 x 93
Line 358 ⟶ 453:
=={{header|C sharp|C#}}==
{{trans|C}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace RosettaVampireNumber
Line 447 ⟶ 542:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 482 ⟶ 577:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <utility>
#include <algorithm>
Line 489 ⟶ 584:
#include <string>
#include <cmath>
 
bool isVampireNumber( long number, std::vector<std::pair<long, long> > & solution ) {
std::ostringstream numberstream ;
Line 497 ⟶ 592:
int fanglength = numberstring.length( ) / 2 ;
long start = static_cast<long>( std::pow( 10 , fanglength - 1 ) ) ;
long end = start * 10sqrt(number) ;
for ( long i = start ; i < (= end - start ) / 2 ; i++ ) {
if ( number % i == 0 ) {
long quotient = number / i ;
if ( ( i % 10 == 0 ) && ( quotient % 10 == 0 ) )
return falsecontinue ;
numberstream.str( "" ) ; //clear the number stream
numberstream << i << quotient ;
Line 515 ⟶ 610:
return !solution.empty( ) ;
}
 
void printOut( const std::pair<long, long> & solution ) {
std::cout << "[ " << solution.first << " , " << solution.second << " ]" ;
}
 
int main( ) {
int vampireNumbersFound = 0 ;
Line 529 ⟶ 624:
for ( long num = start ; num < end ; num++ ) {
if ( isVampireNumber( num , solutions ) ) {
vampireNumbersFound++ ;
std::cout << vampireNumbersFound << " :" << num << " is a vampire number! These are the fangs:\n" ;
std::for_each( solutions.begin( ) , solutions.end( ) , printOut ) ;
std::cout << "\n_______________" << std::endl ;
solutions.clear( ) ;
vampireNumbersFound++ ;
if ( vampireNumbersFound == 25 )
break ;
Line 542 ⟶ 637:
std::vector<long> testnumbers ;
testnumbers.push_back( 16758243290880 ) ;
testnumbers.push_back( 249590173486524959017348650 ) ;
testnumbers.push_back( 14593825548650 ) ;
for ( std::vector<long>::const_iterator svl = testnumbers.begin( ) ;
Line 556 ⟶ 651:
}
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>
<pre>0 :1260 is a vampire number! These are the fangs:
1 :1260 is a vampire number! These are the fangs:
[ 21 , 60 ]
_______________
12 :1395 is a vampire number! These are the fangs:
[ 15 , 93 ]
_______________
23 :1435 is a vampire number! These are the fangs:
[ 35 , 41 ][ 41 , 35 ]
_______________
34 :1530 is a vampire number! These are the fangs:
[ 30 , 51 ]
_______________
45 :1827 is a vampire number! These are the fangs:
[ 21 , 87 ]
_______________
56 :2187 is a vampire number! These are the fangs:
[ 27 , 81 ]
_______________
67 :1025106880 is a vampire number! These are the fangs:
[ 80 , 86 ]
_______________
8 :102510 is a vampire number! These are the fangs:
[ 201 , 510 ]
_______________
79 :104260 is a vampire number! These are the fangs:
[ 260 , 401 ][ 401 , 260 ]
_______________
810 :105210 is a vampire number! These are the fangs:
[ 210 , 501 ]
_______________
911 :105264 is a vampire number! These are the fangs:
[ 204 , 516 ]
_______________
1012 :105750 is a vampire number! These are the fangs:
[ 150 , 705 ]
_______________
1113 :108135 is a vampire number! These are the fangs:
[ 135 , 801 ]
_______________
1214 :110758 is a vampire number! These are the fangs:
[ 158 , 701 ]
_______________
1315 :115672 is a vampire number! These are the fangs:
[ 152 , 761 ]
_______________
1416 :116725 is a vampire number! These are the fangs:
[ 161 , 725 ]
_______________
1517 :117067 is a vampire number! These are the fangs:
[ 167 , 701 ]
_______________
1618 :118440 is a vampire number! These are the fangs:
[ 141 , 840 ]
_______________
1719 :123354120600 is a vampire number! These are the fangs:
[ 201 , 600 ]
_______________
20 :123354 is a vampire number! These are the fangs:
[ 231 , 534 ]
_______________
1821 :124483 is a vampire number! These are the fangs:
[ 281 , 443 ][ 443 , 281 ]
_______________
1922 :125248 is a vampire number! These are the fangs:
[ 152 , 824 ]
_______________
2023 :125433 is a vampire number! These are the fangs:
[ 231 , 543 ]
_______________
2124 :125460 is a vampire number! These are the fangs:
[ 204 , 615 ][ 246 , 510 ]
_______________
2225 :125500 is a vampire number! These are the fangs:
[ 251 , 500 ]
_______________
23 :126027 is a vampire number! These are the fangs:
[ 201 , 627 ]
_______________
24 :126846 is a vampire number! These are the fangs:
[ 261 , 486 ]
_______________
16758243290880 is a vampire number! The fangs:
[ 1982736 , 8452080 ][ 2123856 , 7890480 ][ 2751840 , 6089832 ][ 2817360 , 5948208 ]
249590173486524959017348650 is a vampire number! The fangs:
[ 2947052947050 , 8469153 ][ 4125872949705 , 8461530 ][ 4125870 , 6049395 ][ 4129587 , 6043950 ][ 4230765 , 5899410 ]
14593825548650 is not a vampire number!
</pre>
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn factor-pairs [n]
(for [x (range 2 (Math/sqrt n))
:when (zero? (mod n x))]
Line 663 ⟶ 759:
 
(doseq [n [16758243290880, 24959017348650, 14593825548650]]
(println (or (vampiric? n) (str n " is not vampiric."))))</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
 
<langsyntaxhighlight lang="lisp">(defun trailing-zerop (number)
"Is the lowest digit of `number' a 0"
(zerop (rem number 10)))
Line 727 ⟶ 823:
(when fangs
(print-vampire candidate fangs))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 768 ⟶ 864:
{{trans|Clojure}}
(Runtime about 1.34 seconds with dmd.)
<langsyntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.typecons, std.conv;
 
auto fangs(in long n) pure nothrow @safe {
Line 786 ⟶ 882:
14593825548650].map!fangs))
writefln("%d: (%(%(%s %)) (%))", v[]);
}</langsyntaxhighlight>
{{out}}
<pre>1260: (21 60)
Line 820 ⟶ 916:
{{trans|C}}
(Runtime about 0.27 seconds with dmd, about 0.25 seconds with ldc2 compilers.)
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.algorithm, std.array, std.traits;
 
T[N] pows(T, size_t N)() pure nothrow @safe @nogc {
Line 911 ⟶ 1,007:
showFangs(bi, fs);
}
}</langsyntaxhighlight>
{{out}}
<pre> 2: 1395 = 15 x 93
Line 941 ⟶ 1,037:
24959017348650 = 2947050 x 8469153 = 2949705 x 8461530 = 4125870 x 6049395 = 4129587 x 6043950 = 4230765 x 5899410
14593825548650 is not vampiric</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func dtally x .
while x > 0
t += bitshift 1 (x mod 10 * 6)
x = x div 10
.
return t
.
proc fangs x . f[] .
f[] = [ ]
nd = floor log10 x + 1
if nd mod 2 = 1
return
.
nd = nd div 2
lo = higher pow 10 (nd - 1) (x + pow 10 nd - 2) div (pow 10 nd - 1)
hi = lower (x / lo) sqrt x
t = dtally x
for a = lo to hi
b = x div a
if a * b = x and (a mod 10 > 0 or b mod 10 > 0) and t = dtally a + dtally b
f[] &= a
.
.
.
proc show_fangs x f[] . .
write x & " "
for f in f[]
write " = " & f & " x " & x div f
.
print ""
.
x = 1
while n < 25
fangs x f[]
if len f[] > 0
n += 1
write n & ": "
show_fangs x f[]
.
x += 1
.
bigs[] = [ 16758243290880 24959017348650 14593825548650 ]
for x in bigs[]
fangs x f[]
if len f[] > 0
show_fangs x f[]
else
print x & " is not vampiric"
.
.
</syntaxhighlight>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,055 ⟶ 1,206:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,098 ⟶ 1,249:
=={{header|Elixir}}==
{{works with|Elixir|1.1+}}
<langsyntaxhighlight lang="elixir">defmodule Vampire do
def factor_pairs(n) do
first = trunc(n / :math.pow(10, div(char_len(n), 2)))
Line 1,139 ⟶ 1,290:
end
 
Vampire.task</langsyntaxhighlight>
 
{{out}}
Line 1,173 ⟶ 1,324:
14593825548650 is not a vampire number!
</pre>
 
=={{header|Factor}}==
<syntaxhighlight 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</syntaxhighlight>
{{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|Forth}}==
{{works with|gforth|0.7.3}}
 
<syntaxhighlight lang="Forth">: sqrt ( u -- sqrt ) ( Babylonian method )
dup 2/ ( first square root guess is half )
dup 0= if drop exit then ( sqrt[0]=0, sqrt[1]=1 )
begin dup >r 2dup / r> + 2/ ( stack: square old-guess new-guess )
2dup > while ( as long as guess is decreasing )
nip repeat ( forget old-guess and repeat )
drop nip ;
 
: ndigits ( n -- n ) 10 / dup 0<> if recurse then 1+ ;
 
: dtally ( n -- n )
10 /mod
dup 0<> if recurse then
swap 6 * 1 swap lshift + ;
 
: ?product ( x a b -- f ) * = ;
: ?dtally ( x a b -- f ) dtally rot dtally rot dtally rot + = ;
: ?ndigits ( a b -- f ) ndigits swap ndigits = ;
: ?0trail ( a b -- f ) 10 mod 0= swap 10 mod 0= and invert ;
 
: ?fang ( x a -- f )
2dup / 2>r
dup 2r@ ?product
swap 2r@ ?dtally and
2r@ ?ndigits and
2r> ?0trail and ;
 
: next-fang ( n a -- false | a true )
over sqrt swap 1+ ?do
dup i ?fang if drop i true unloop exit then
loop drop false ;
 
: ?vampire ( n -- false | a true ) 0 next-fang ;
: next-vampire ( n -- n a ) begin 1+ dup ?vampire until ;
 
: .product ( n a -- ) dup . ." x " / . ." = " ;
 
: .vampire ( n a -- )
cr
begin 2dup .product
over swap next-fang
while repeat
. ;
 
: .fangs ( n -- ) dup ?vampire if .vampire else cr . ." is not vampiric." then ;
 
: vampires ( n -- )
1 swap
0 do next-vampire over swap .vampire loop drop ;
 
25 vampires
16758243290880 .fangs
24959017348650 .fangs
14593825548650 .fangs</syntaxhighlight>
 
{{out}}<pre>25 vampires
21 x 60 = 1260
15 x 93 = 1395
35 x 41 = 1435
30 x 51 = 1530
21 x 87 = 1827
27 x 81 = 2187
80 x 86 = 6880
201 x 510 = 102510
260 x 401 = 104260
210 x 501 = 105210
204 x 516 = 105264
150 x 705 = 105750
135 x 801 = 108135
158 x 701 = 110758
152 x 761 = 115672
161 x 725 = 116725
167 x 701 = 117067
141 x 840 = 118440
201 x 600 = 120600
231 x 534 = 123354
281 x 443 = 124483
152 x 824 = 125248
231 x 543 = 125433
204 x 615 = 246 x 510 = 125460
251 x 500 = 125500 ok
16758243290880 .fangs
1982736 x 8452080 = 2123856 x 7890480 = 2751840 x 6089832 = 2817360 x 5948208 = 16758243290880 ok
24959017348650 .fangs
2947050 x 8469153 = 2949705 x 8461530 = 4125870 x 6049395 = 4129587 x 6043950 = 4230765 x 5899410 = 24959017348650 ok
14593825548650 .fangs
14593825548650 is not vampiric. ok</pre>
 
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">'Vampire numbers.
'FreeBASIC version 24. Windows
'Vampire.bas
Line 1,279 ⟶ 1,601:
Print "Completed in ";
Print t2-t1;" Seconds"
Sleep</langsyntaxhighlight>
{{out}}
<pre>First 28 Vampire numbers
Line 1,330 ⟶ 1,652:
 
Completed in 1.286374813709699 Seconds</pre>
 
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,422 ⟶ 1,745:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,470 ⟶ 1,793:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List (sort)
import Control.Arrow ((&&&))
 
-- VAMPIRE NUMBERS ------------------------------------------------------------
vampires :: [Int]
vampires = filter ((0 <)not . lengthnull . fangs) [1 ..]
 
fangs :: Int -> [(Int, Int)]
Line 1,518 ⟶ 1,841:
mapM
(print . ((,) <*>) fangs)
(take 25 vampires ++ [16758243290880, 24959017348650, 14593825548650])</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,554 ⟶ 1,877:
The following works in both languages.
 
<langsyntaxhighlight lang="unicon">procedure main()
write("First 25 vampire numbers and their fangs:")
every fangs := vampire(n := seq())\25 do write(right(n,20),":",fangs)
Line 1,579 ⟶ 1,902:
every (s1 := "", c := !cset(s)) do every find(c, s) do s1 ||:= c
return s1
end</langsyntaxhighlight>
 
Output:
Line 1,617 ⟶ 1,940:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
Filter=: (#~`)(`:6)
odd =: 2&|
Line 1,658 ⟶ 1,981:
fangs f. NB. <laugh>
((10&(#.^:_1)@:[ -:&(/:~) ,&(10&(#.^:_1))/@:])"0 1 # ]) ((% ,. ]) (#~ (0 < [: # :[: 0 -.~ 10&|)"1)@:(((> <.@:%:)~ # ]) (((-: :[:@:(# :[:)@:(10&(#.^:_1))@:[ = # :[:@:(10&(#.^:_1))&>@:]) # ]) ([: ([: /:~ [: */"1 ([: x: [) ^"1 [: > [: , [: { [: <@:i.@>: ])/ __ q: ]))))
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.HashSet;
 
Line 1,709 ⟶ 2,032:
}
}
}</langsyntaxhighlight>
Output:
<pre>1260: [21, 60]
Line 1,749 ⟶ 2,072:
 
===Alternative version===
<langsyntaxhighlight lang="java">public class VampireNumber {
 
public static void main(String args[]) {
Line 1,818 ⟶ 2,141:
return total;
}
}</langsyntaxhighlight>
 
Output:
Line 1,852 ⟶ 2,175:
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'''
 
<syntaxhighlight 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):
limit($n; range(1; infinite)
| . as $i
| vampire_factors
| select(length>0)
| "\($i):\t\(.)" );
 
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</syntaxhighlight>
{{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}}==
'''Functions'''
<syntaxhighlight lang="julia">
<lang Julia>
function divisors{T<:Integer}(n::T)
!isprime(n) || return [one(T), n]
Line 1,890 ⟶ 2,295:
return (isvampire, fangs)
end
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">using Printf
<lang Julia>
 
function showvampire{T<:Integer}(i::T, n::T, fangs::Array{T,2})
s = @sprintf "%6d %14d %s\n" i n join(fangs[1,:], "\u00d7")
Line 1,930 ⟶ 2,336:
end
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,978 ⟶ 2,384:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1
 
data class Fangs(val fang1: Long = 0L, val fang2: Long = 0L)
Line 2,073 ⟶ 2,479:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,109 ⟶ 2,515:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[VampireQ]
VampireQ[num_Integer] := Module[{poss, divs},
divs = Select[Divisors[num], # <= Sqrt[num] &];
Line 2,136 ⟶ 2,542:
False
]
]</langsyntaxhighlight>
Testing out the function:
<langsyntaxhighlight Mathematicalang="mathematica">Reap[Scan[VampireQ,Range[126027]]][[2,1]]//Grid
Reap[VampireQ[#]] & /@ {16758243290880, 24959017348650, 14593825548650} // Grid</langsyntaxhighlight>
{{out}}
<pre>
Line 2,171 ⟶ 2,577:
True {{{24959017348650,{{2947050,8469153},{2949705,8461530},{4125870,6049395},{4129587,6043950},{4230765,5899410}}}}}
False {}</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import algorithm, math, sequtils, strformat, strutils, sugar
 
const Pow10 = collect(newSeq, for n in 0..18: 10 ^ n)
 
template isOdd(n: int): bool = (n and 1) != 0
 
proc fangs(n: Positive): seq[(int, int)] =
## Return the list fo fangs of "n" (empty if "n" is not vampiric).
let nDigits = sorted($n)
if nDigits.len.isOdd: return @[]
let fangLen = nDigits.len div 2
let inf = Pow10[fangLen - 1]
let sup = inf * 10 - 1
for d in inf..sup:
if n mod d != 0: continue
let q = n div d
if q < d: return
let dDigits = $d
let qDigits = $q
if qDigits.len > fangLen: continue
if qDigits.len < fangLen: return
if nDigits != sorted(dDigits & qDigits): continue
if dDigits[^1] != '0' or qDigits[^1] != '0':
# Finally, "n" is vampiric. Add the fangs to the result.
result.add (d, q)
 
 
echo "First 25 vampire numbers with their fangs:"
var count = 0
var n = 10
var limit = 100
while count != 25:
let fangList = n.fangs
if fangList.len != 0:
inc count
echo &"{count:2}: {n:>6} = ", fangList.mapIt(&"{it[0]:3} × {it[1]:3}").join(" = ")
inc n
if n == limit:
n *= 10
limit *= 10
 
echo()
for n in [16_758_243_290_880, 24_959_017_348_650, 14_593_825_548_650]:
let fangList = n.fangs
if fangList.len == 0:
echo &"{n} is not vampiric."
else:
echo &"{n} = ", fangList.mapIt(&"{it[0]} × {it[1]}").join(" = ")</syntaxhighlight>
 
{{out}}
<pre>First 25 vampire numbers with their fangs:
1: 1260 = 21 × 60
2: 1395 = 15 × 93
3: 1435 = 35 × 41
4: 1530 = 30 × 51
5: 1827 = 21 × 87
6: 2187 = 27 × 81
7: 6880 = 80 × 86
8: 102510 = 201 × 510
9: 104260 = 260 × 401
10: 105210 = 210 × 501
11: 105264 = 204 × 516
12: 105750 = 150 × 705
13: 108135 = 135 × 801
14: 110758 = 158 × 701
15: 115672 = 152 × 761
16: 116725 = 161 × 725
17: 117067 = 167 × 701
18: 118440 = 141 × 840
19: 120600 = 201 × 600
20: 123354 = 231 × 534
21: 124483 = 281 × 443
22: 125248 = 152 × 824
23: 125433 = 231 × 543
24: 125460 = 204 × 615 = 246 × 510
25: 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 vampiric.</pre>
 
=={{header|PARI/GP}}==
 
<lang parigp>fang(n)=my(v=digits(n),u=List());if(#v%2,return([]));fordiv(n,d,if(#Str(d)==#v/2 && #Str(n/d)==#v/2 && vecsort(v)==vecsort(concat(digits(d),digits(n/d))) && (d%10 || (n/d)%10), if(d^2>n,return(Vec(u))); listput(u, d))); Vec(u)
{{Lines too long|PARI/GP}}
 
<syntaxhighlight lang="parigp">fang(n)=my(v=digits(n),u=List());if(#v%2,return([]));fordiv(n,d,if(#Str(d)==#v/2 && #Str(n/d)==#v/2 && vecsort(v)==vecsort(concat(digits(d),digits(n/d))) && (d%10 || (n/d)%10), if(d^2>n,return(Vec(u))); listput(u, d))); Vec(u)
k=25;forstep(d=4,6,2,for(n=10^(d-1),10^d-1,f=fang(n); for(i=1,#f,print(n" "f[i]" "n/f[i]); if(i==#f && k--==0,return))))
print();v=[16758243290880, 24959017348650, 14593825548650];
for(i=1,#v,f=fang(v[i]); for(j=1,#f, print(v[i]" "f[j]" "v[i]/f[j])))</langsyntaxhighlight>
Output:
<pre>1260 21 60
Line 2,217 ⟶ 2,708:
=={{header|Perl}}==
 
ByThe thetrailing way, the lastzeros condition (trailing zeros) is first triggered when searching for the 26th vampire number.
 
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 2,251 ⟶ 2,742:
}
 
say join ' ', $_, map "[@$_]", fangs($_) for 16758243290880, 24959017348650, 14593825548650;</langsyntaxhighlight>
 
A faster version, using the `divisors()` function from the <code>ntheory</code> library.
=={{header|Perl 6}}==
{{trans|Sidef}}
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use ntheory qw(sqrtint logint divisors);
 
sub is_vampire {
my ($n) = @_;
 
return if (length($n) % 2 or $n < 0);
 
my $l1 = 10**logint(sqrtint($n), 10);
my $l2 = sqrtint($n);
 
my $s = join('', sort split(//, $n));
 
<lang perl6>sub is_vampire (Int $num) {
my $digits = $num.comb.sort;
my @fangs;
for vfactors($num) -> $this {
my $that = $num div $this;
@fangs.push("$this x $that") if
!($this %% 10 && $that %% 10) and
($this ~ $that).comb.sort eq $digits;
}
return @fangs;
}
 
foreach my $d (divisors($n)) {
constant @vampires = gather for 1 .. * -> $n {
next if $n.log(10).floor %% 2;
my @fangs = is_vampire($n);
take "$n: { @fangs.join(', ') }" if @fangs.elems;
}
 
$d < $l1 and next;
say "\nFirst 25 Vampire Numbers:\n";
$d > $l2 and last;
 
my $t = $n / $d;
.say for @vampires[^25];
 
next if ($d % 10 == 0 and $t % 10 == 0);
say "\nIndividual tests:\n";
next if (join('', sort split(//, "$d$t")) ne $s);
 
push @fangs, [$d, $t];
for 16758243290880, 24959017348650, 14593825548650 {
print "$_: ";
my @fangs = is_vampire($_);
if @fangs.elems {
say @fangs.join(', ');
} else {
say 'is not a vampire number.';
}
 
return @fangs;
}
 
print "First 25 Vampire Numbers:\n";
sub vfactors (Int $n) {
map { $_ if $n %% $_ }, 10**$n.sqrt.log(10).floor .. $n.sqrt.ceiling;
}</lang>
 
for (my ($n, $i) = (1, 1) ; $i <= 25 ; ++$n) {
if (my @fangs = is_vampire($n)) {
printf("%2d. %6s : %s\n", $i++, $n, join(' ', map { "[@$_]" } @fangs));
}
}
 
print "\nIndividual tests:\n";
 
foreach my $n (16758243290880, 24959017348650, 14593825548650) {
my @fangs = is_vampire($n);
print("$n: ", (@fangs ? join(' ', map { "[@$_]" } @fangs)
: "is not a vampire number"), "\n");
}</syntaxhighlight>
{{out}}
<pre>
First 25 Vampire Numbers:
1. 1260 : [21 60]
 
2. 1395 : [15 93]
1260: 21 x 60
3. 1435 : [35 41]
1395: 15 x 93
4. 1530 : [30 51]
1435: 35 x 41
5. 1827 : [21 87]
1530: 30 x 51
6. 2187 : [27 81]
1827: 21 x 87
7. 6880 : [80 86]
2187: 27 x 81
8. 102510 : [201 510]
6880: 80 x 86
9. 104260 : [260 401]
102510: 201 x 510
10. 105210 : [210 501]
104260: 260 x 401
11. 105264 : [204 516]
105210: 210 x 501
12. 105750 : [150 705]
105264: 204 x 516
13. 108135 : [135 801]
105750: 150 x 705
14. 110758 : [158 701]
108135: 135 x 801
15. 115672 : [152 761]
110758: 158 x 701
16. 116725 : [161 725]
115672: 152 x 761
17. 117067 : [167 701]
116725: 161 x 725
18. 118440 : [141 840]
117067: 167 x 701
19. 120600 : [201 600]
118440: 141 x 840
20. 123354 : [231 534]
120600: 201 x 600
21. 124483 : [281 443]
123354: 231 x 534
22. 125248 : [152 824]
124483: 281 x 443
23. 125433 : [231 543]
125248: 152 x 824
24. 125460 : [204 615] [246 510]
125433: 231 x 543
25. 125500 : [251 500]
125460: 204 x 615, 246 x 510
125500: 251 x 500
 
Individual tests:
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|Phix}}==
16758243290880: 1982736 x 8452080, 2123856 x 7890480, 2751840 x 6089832, 2817360 x 5948208
<!--<syntaxhighlight lang="phix">(phixonline)-->
24959017348650: 2947050 x 8469153, 2949705 x 8461530, 4125870 x 6049395, 4129587 x 6043950, 4230765 x 5899410
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
14593825548650: is not a vampire number.
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (for in)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">vampire</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">vs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vs</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- even length</span>
<span style="color: #000000;">vs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vs</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">/</span><span style="color: #000000;">i</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">s2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">[$]!=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">or</span> <span style="color: #000000;">s2</span><span style="color: #0000FF;">[$]!=</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">and</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">&</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">vs</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i2</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first 26 vampire numbers and their fangs:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">found</span><span style="color: #0000FF;"><</span><span style="color: #000000;">26</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">vampire</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %d: %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">found</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span> <span style="color: #7060A8;">in</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">16758243290880</span><span style="color: #0000FF;">,</span><span style="color: #000000;">24959017348650</span><span style="color: #0000FF;">,</span><span style="color: #000000;">14593825548650</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">vampire</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">={}?</span><span style="color: #008000;">"not a vampire number"</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
The first 26 vampire numbers and their fangs:
1: 1260: {{21,60}}
2: 1395: {{15,93}}
3: 1435: {{35,41}}
4: 1530: {{30,51}}
5: 1827: {{21,87}}
6: 2187: {{27,81}}
7: 6880: {{80,86}}
8: 102510: {{201,510}}
9: 104260: {{260,401}}
10: 105210: {{210,501}}
11: 105264: {{204,516}}
12: 105750: {{150,705}}
13: 108135: {{135,801}}
14: 110758: {{158,701}}
15: 115672: {{152,761}}
16: 116725: {{161,725}}
17: 117067: {{167,701}}
18: 118440: {{141,840}}
19: 120600: {{201,600}}
20: 123354: {{231,534}}
21: 124483: {{281,443}}
22: 125248: {{152,824}}
23: 125433: {{231,543}}
24: 125460: {{204,615},{246,510}}
25: 125500: {{251,500}}
26: 126027: {{201,627}}
 
16758243290880: {{1982736,8452080},{2123856,7890480},{2751840,6089832},{2817360,5948208}}
24959017348650: {{2947050,8469153},{2949705,8461530},{4125870,6049395},{4129587,6043950},{4230765,5899410}}
14593825548650: not a vampire number
</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">EnableExplicit
DisableDebugger
 
Line 2,391 ⟶ 2,969:
EndIf
Next
Return</langsyntaxhighlight>
{{out}}
<pre>The first 25 Vampire numbers...
Line 2,441 ⟶ 3,019:
=={{header|Python}}==
This routine finds ''all'' the fangs for a number.
<langsyntaxhighlight lang="python">from __future__ import division
 
import math
Line 2,517 ⟶ 3,095:
for n in (16758243290880, 24959017348650, 14593825548650):
fangpairs = vampire(n)
print('%i: %r' % (n, fangpairs))</langsyntaxhighlight>
{{out}}
<pre>First 25 vampire numbers
Line 2,553 ⟶ 3,131:
This alternative solution is not fast but it's short:
{{trans|Clojure}}
<langsyntaxhighlight lang="python">from math import sqrt
from itertools import imap, ifilter, islice, count
 
Line 2,575 ⟶ 3,153:
 
for n in [16758243290880, 24959017348650, 14593825548650]:
print vampiricQ(n) or str(n) + " is not vampiric."</langsyntaxhighlight>
{{out}}
<pre>(1260, [(21, 60)])
Line 2,610 ⟶ 3,188:
Naive implementation, but notice the sequence-filter/sequence-map composition --
it's a good way to "find the first n numbers meeting predicate p?":
<langsyntaxhighlight lang="racket">#lang racket
 
;; chock full of fun... including divisors
Line 2,643 ⟶ 3,221:
(displayln (vampire?-and-fangs 16758243290880))
(displayln (vampire?-and-fangs 24959017348650))
(displayln (vampire?-and-fangs 14593825548650))</langsyntaxhighlight>
Output:
<pre>First 25 vampire numbers:
Line 2,675 ⟶ 3,253:
(24959017348650 (2947050 8469153) (2949705 8461530) (4125870 6049395) (4129587 6043950) (4230765 5899410))
#f</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>sub is_vampire (Int $num) {
my $digits = $num.comb.sort;
my @fangs;
(10**$num.sqrt.log(10).floor .. $num.sqrt.ceiling).map: -> $this {
next if $num % $this;
my $that = $num div $this;
next if $this %% 10 && $that %% 10;
@fangs.push("$this x $that") if ($this ~ $that).comb.sort eq $digits;
}
@fangs
}
 
constant @vampires = flat (3..*).map: -> $s, $e {
(10**$s .. 10**$e).hyper.map: -> $n {
next unless my @fangs = is_vampire($n);
"$n: { @fangs.join(', ') }"
}
}
 
say "\nFirst 25 Vampire Numbers:\n";
 
.say for @vampires[^25];
 
say "\nIndividual tests:\n";
 
.say for (16758243290880, 24959017348650, 14593825548650).hyper(:1batch).map: {
"$_: " ~ (is_vampire($_).join(', ') || 'is not a vampire number.')
}</syntaxhighlight>
 
<pre>First 25 Vampire Numbers:
 
1260: 21 x 60
1395: 15 x 93
1435: 35 x 41
1530: 30 x 51
1827: 21 x 87
2187: 27 x 81
6880: 80 x 86
102510: 201 x 510
104260: 260 x 401
105210: 210 x 501
105264: 204 x 516
105750: 150 x 705
108135: 135 x 801
110758: 158 x 701
115672: 152 x 761
116725: 161 x 725
117067: 167 x 701
118440: 141 x 840
120600: 201 x 600
123354: 231 x 534
124483: 281 x 443
125248: 152 x 824
125433: 231 x 543
125460: 204 x 615, 246 x 510
125500: 251 x 500
 
Individual tests:
 
16758243290880: 1982736 x 8452080, 2123856 x 7890480, 2751840 x 6089832, 2817360 x 5948208
24959017348650: 2947050 x 8469153, 2949705 x 8461530, 4125870 x 6049395, 4129587 x 6043950, 4230765 x 5899410
14593825548650: is not a vampire number.</pre>
 
=={{header|REXX}}==
Note: &nbsp; if the argument is negative, its absolute value is used to test if <u>that</u> single number is vampiric.
<langsyntaxhighlight lang="rexx">/*REXX program displays N vampire numbers, or verifies if a number is vampiric. */
numericparse digitsarg 20N . /*be able to handle gihugic numbers./*obtain optional argument from the CL.*/
parse arg N .; if N=='' | N=="," then N= 25 /*Not specified? Then use the default.*/
!.0= 1260; !.1= 11453481; !.2= 115672; !.3= 124483; !.4= 105264 /*lowest #, dig.*/
!.5= 1395; !.6=126846; 126846; !.7=1827; 1827; !.8= 110758; !.9= 156289 /* " " " */
#L=0 length(N); aN= abs(N) /*L: length of N; aN: absolute /*num. of vampire numbers found, so farvalue*/
numeric digits max(9, length(aN) ) /*be able to handle ginormus numbers. */
# = 0 /*#: count of vampire numbers (so far)*/
if N>0 then do j=1260 until # >= N /*search until N vampire numbers found.*/
if length(j) // 2 then do; j= j*10 - 1; iterate; /*bump J endto even /*adjust Jlength*/
_=right(J,1); if j<!._ then iterate end /*is number[↑] tenable basedcheck onif lastodd. dig? */
f=vampire(parse var j); if f=='' then-1 _ iterate /*Areobtain fangsthe null?last decimal digit Yes,of not vampireJ. */
#=#+1 if j<!._ then iterate /*bump the vampire count, Vlad. is number tenable based on last dig? */
sayf= 'vampire number' right(#,length(N)j) "is: " j', /*obtain the fangs=' f of J. */
if f=='' then iterate /*Are fangs null? Yes, not vampire. */
#= # + 1 /*bump the vampire count, Vlad. */
say right('vampire number', 20) right(#, L) "is: " right(j, 9)', fangs=' f
end /*j*/ /* [↑] process a range of numbers. */
else do; Nf=abs vampire(NaN); f=vampire(N) /* [↓] process onea number; getobtain fangs.*/
if f=='' then say N aN " isn't a vampire number."
else say N aN " is a vampire number, fangs=" f
end
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
vampire: procedure; parse arg ?x,, $. !!a bot; L= length(?x); /*get arg; compute iflen Lof X*//2 then return !!
w=if L%//2 then return '' /* [↑] is L an odd? length? Then ¬vampire.*/
do k=1 for L; _= substr(?x, k, 1); $._= $._ || _; end /*k*/_
do m=0 end for 10; bot=bot || $.m; end /*mk*/
topw=left( reverse(bot),L % 2 w); bot=left(bot, w) /*determine%: is REXX's integer limits of÷ search*/
inc=?//2 + 1 do m=0 for 10; bot= bot || /*? is odd? INC=2$. No? INC=1*/m
start=max(bot, 10**(w-1)); if inc=2 then if start//2==0 then start=start+1 end /*m*/
top= left( reverse(bot), w)
/* [↑] odd START if odd INC*/
bot= left(bot, w) do d=start to min(top, 10/**w-1)determine limits byof incsearch*/
inc= x // 2 + if1 ?//d\==0 then iterate /*X is odd? INC=2. No? INC=1*/
beg= max(bot, 10 ** (w-1) ) if verify(d, ?) \==0 then iterate /*calculate where to begin.*/
if inc==2 then if beg//2==0 then qbeg=?%d; beg + 1 /*possibly adjust the if d>q then iteratebegin.*/
if q*d//9\==(q+d)//9 then iterate /*modulo 9[↑] congruence test.odd BEG if odd INC*/
do d=beg if verifyto min(qtop, ?10**w - 1) \==0 by inc /*use smart BEG, END, thenINC. iterate*/
if right(q, 1)x // d \==0 then if right(d, 1)==0 then iterate /*X not ÷ by D? Then skip,*/
q= x if% length(q)\==wd; if d>q then iterate /*is D > Q " " */
if length(q) dq\==d ||w q; t then iterate /*Len of Q ¬= W? Then skip.*/
if q*d//9 do i\==1 (q+d)//9 for then L;iterate p=pos(/*modulo substr(dq,9 i,congruence 1),test. t)*/
parse var q '' -1 _ if p==0 then iterate/*get d;last decimal dig. of t=delstr(t, p, 1)Q*/
if _== 0 then if right(d, 1) == 0 end then /*i*/iterate
dq= d !!=!! || '['d"∙"q']'
t= x; end /*d*/ do i=1 for L; p= pos( substr(dq, i, 1), t)
if p==0 then iterate d; t= delstr(t, p, 1)
return !!</lang>
end /*i*/
'''output''' when using the default input:
a= a '['d"∙"q'] ' /*construct formatted fangs.*/
<pre style="height:50ex">
end /*d*/ /* [↑] ∙ is a round bullet*/
vampire number 1 is: 1260, fangs= [21∙60]
return a /*return formatted fangs.*/</syntaxhighlight>
vampire number 2 is: 1395, fangs= [15∙93]
{{out|output|text=&nbsp; when using the default input:}}
vampire number 3 is: 1435, fangs= [35∙41]
<pre>
vampire number 4 is: 1530, fangs= [30∙51]
vampire number 51 is: 1827 1260, fangs= [21∙8721∙60]
vampire number 62 is: 2187 1395, fangs= [27∙8115∙93]
vampire number 73 is: 6880 1435, fangs= [80∙8635∙41]
vampire number 84 is: 102510 1530, fangs= [201∙51030∙51]
vampire number 95 is: 104260 1827, fangs= [260∙40121∙87]
vampire number 10 6 is: 105210 2187, fangs= [210∙50127∙81]
vampire number 11 7 is: 105264 6880, fangs= [204∙51680∙86]
vampire number 12 8 is: 105750 102510, fangs= [150∙705201∙510]
vampire number 13 9 is: 108135 104260, fangs= [135∙801260∙401]
vampire number 1410 is: 110758 105210, fangs= [158∙701210∙501]
vampire number 1511 is: 115672 105264, fangs= [152∙761204∙516]
vampire number 1612 is: 116725 105750, fangs= [161∙725150∙705]
vampire number 1713 is: 117067 108135, fangs= [167∙701135∙801]
vampire number 1814 is: 118440 110758, fangs= [141∙840158∙701]
vampire number 1915 is: 120600 115672, fangs= [201∙600152∙761]
vampire number 2016 is: 123354 116725, fangs= [231∙534161∙725]
vampire number 2117 is: 124483 117067, fangs= [281∙443167∙701]
vampire number 2218 is: 125248 118440, fangs= [152∙824141∙840]
vampire number 2319 is: 125433 120600, fangs= [231∙543201∙600]
vampire number 2420 is: 125460 123354, fangs= [204∙615] [246∙510231∙534]
vampire number 2521 is: 125500 124483, fangs= [251∙500281∙443]
vampire number 22 is: 125248, fangs= [152∙824]
vampire number 23 is: 125433, fangs= [231∙543]
vampire number 24 is: 125460, fangs= [204∙615] [246∙510]
vampire number 25 is: 125500, fangs= [251∙500]
</pre>
'''{{out|output'''|text=&nbsp; when using the input of: &nbsp; <tt> -16758243290880 </tt>}}
<pre>
16758243290880 is a vampire number, fangs= [1982736∙8452080] [2123856∙7890480] [2751840∙6089832] [2817360∙5948208]
</pre>
'''{{out|output'''|text=&nbsp; when using the input of: &nbsp; <tt> -24959017348650 </tt>}}
<pre>
24959017348650 is a vampire number, fangs= [2947050∙8469153] [2949705∙8461530] [4125870∙6049395] [4129587∙6043950] [4230765∙5899410]
</pre>
'''{{out|output'''|text=&nbsp; when using the input of: &nbsp; <tt> -14593825548650 </tt>}}
<pre>
14593825548650 isn't a vampire number.
Line 2,762 ⟶ 3,415:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Vampire number
# Date : 2018/02/01
# Author : Gal Zsolt [~ CalmoSoft ~]
# Email : <calmosoft@gmail.com>
 
for p = 10 to 127000
Line 2,845 ⟶ 3,495:
next
return alist
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,871 ⟶ 3,521:
125433: [231,543]
125460: [204,615]
[246,510]
125500: [251,500]
126027: [201,627]
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
« 0 10 NDPUN →LIST
'''WHILE''' OVER '''REPEAT'''
SWAP 10 IDIV2 1 +
ROT SWAP DUP2 GET 1 + PUT
'''END''' NIP
» '<span style="color:blue">DIGCNT</span>' STO
« DUP XPON LASTARG <span style="color:blue">DIGCNT</span> 2 / IP { } DUP
→ n digc fangsize result divs
« n DIVIS SORT
DUP « XPON » MAP
DUP fangsize POS
'''IF''' DUP '''THEN'''
SWAP fangsize 1 + POS 1 - PICK3 SIZE MIN '''FOR''' j
DUP j GET
'''CASE'''
divs OVER POS '''THEN''' DROP '''END'''
n OVER /
DUP XPON fangsize ≠ '''THEN''' DROP2 '''END'''
2 →LIST
DUP 10 MOD { 0 0 } == '''THEN''' DROP '''END'''
DUP « →STR » MAP ∑LIST STR→ <span style="color:blue">DIGCNT</span> digc ≠ '''THEN''' DROP '''END'''
DUP 2 GET 'divs' STO+
'result' STO+
'''END'''
'''NEXT'''
'''ELSE''' DROP2 '''END'''
DROP result
» » '<span style="color:blue">VAMPIRE?</span>' STO
« '''IF''' OVER SIZE '''THEN'''
1 PICK3 SIZE '''FOR''' j
" = " +
OVER j GET +
"*" + OVER j 1 + GET +
2 '''STEP''' NIP
'''ELSE''' DROP2 "Not vampiric" '''END'''
» '<span style="color:blue">V→STR</span>' STO <span style="color:grey">@ ''( { (d1,d2) } n → "n = d1*d2" )''</span>
« { } 10
'''WHILE''' OVER SIZE 8 < '''REPEAT'''
DUP <span style="color:blue">VAMPIRE?</span>
'''IF''' DUP SIZE '''THEN'''
OVER <span style="color:blue">V→STR</span>
ROT SWAP + SWAP
'''ELSE''' DROP '''END'''
1 +
'''IF''' DUP XPON 2 MOD NOT '''THEN''' 10 * '''END'''
'''END''' DROP
{ 16758243290880 24959017348650 14593825548650 }
1 « DUP <span style="color:blue">VAMPIRE?</span> SWAP <span style="color:blue">V→STR</span> » DOLIST
» '<span style="color:blue">TASK</span>' STO
Since the search is very slow, it has been limited to the first eight numbers:
{{out}}
<pre>
2: { "1260 = 60*21" "1395 = 15*93" "1435 = 35*41" "1530 = 30*51" "1827 = 21*87" "2187 = 27*81" "6880 = 80*86" "102510 = 510*201" }
1: { "16758243290880 = 2817360*5948208 = 2751840*6089832 = 2123856*7890480 = 1982736*8452080"
"24959017348650 = 4230765*5899410 = 4129587*6043950 = 4125870*6049395 = 2949705*8461530 = 2947050*8469153"
"Not vampiric" }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight Rubylang="ruby">def factor_pairs n
first = n / (10 ** (n.to_s.size / 2) - 1)
(first .. n ** 0.5).map { |i| [i, n / i] if n % i == 0 }.compact
Line 2,907 ⟶ 3,620:
puts "#{n}:\t#{vf}"
end
end</langsyntaxhighlight>
{{out}}
<pre>
Line 2,939 ⟶ 3,652:
14593825548650 is not a vampire number!
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::cmp::{max, min};
 
static TENS: [u64; 20] = [
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000,
10000000000000000000,
];
 
/// Get the number of digits present in x
fn ndigits(mut x: u64) -> u64 {
let mut n = 0;
 
while x != 0 {
n += 1;
x /= 10;
}
 
n
}
 
fn dtally(mut x: u64) -> u64 {
let mut t = 0;
 
while x != 0 {
t += 1 << ((x % 10) * 6);
x /= 10;
}
 
t
}
 
/// Get a list of all fangs of x. Get only the first divider of each fang. The second one can be found simply with x / fang.
fn fangs(x: u64) -> Vec<u64> {
let mut nd = ndigits(x) as usize;
 
let mut fangs = vec![];
 
if nd & 1 != 1 {
nd /= 2;
 
let lo = max(TENS[nd - 1], (x + TENS[nd] - 2) / (TENS[nd] - 1));
let hi = min(x / lo, (x as f64).sqrt() as u64);
 
let t = dtally(x);
 
for a in lo..(hi + 1) {
let b = x / a;
if a * b == x && ((a % 10) > 0 || b % 10 > 0) && t == dtally(a) + dtally(b) {
fangs.push(a);
}
}
}
 
fangs
}
 
/// Pretty print the fangs of x
fn print_fangs(x: u64, fangs: Vec<u64>) {
print!("{} = ", x);
 
if fangs.is_empty() {
print!("is not vampiric");
} else {
for fang in fangs {
print!("{} x {}, ", fang, x / fang);
}
}
print!("\n");
}
 
fn main() {
println!("The first 25 vampire numbers are :");
 
let mut nfangs = 0;
let mut x = 1;
 
while nfangs < 25 {
let fangs = fangs(x);
if !fangs.is_empty() {
nfangs += 1;
print_fangs(x, fangs);
}
 
x += 1;
}
 
println!("\nSpecial requests :");
 
print_fangs(16758243290880, fangs(16758243290880));
print_fangs(24959017348650, fangs(24959017348650));
print_fangs(14593825548650, fangs(14593825548650));
}
 
#[test]
fn test() {
assert_eq!(
fangs(16758243290880),
vec![1982736, 2123856, 2751840, 2817360]
);
 
assert_eq!(
fangs(24959017348650),
vec![2947050, 2949705, 4125870, 4129587, 4230765]
);
 
assert_eq!(fangs(14593825548650), vec![]);
}</syntaxhighlight>
 
=={{header|Scala}}==
{{works with|Scala|2.9.1}}
<langsyntaxhighlight Scalalang="scala">import Stream._
import math._
import scala.collection.mutable.ListBuffer
Line 2,991 ⟶ 3,830:
 
println("\n"+"elapsed time: "+et+" seconds")
}</langsyntaxhighlight>
Output:
<pre style="height: 30ex; overflow: scroll">1: 1260 = 21 x 60
Line 3,024 ⟶ 3,863:
 
elapsed time: 11 seconds</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func is_vampire (n) {
return [] if n.ilog10.is_even
 
var l1 = n.isqrt.ilog10.ipow10
var l2 = n.isqrt
 
var s = n.digits.sort.join
 
gather {
n.divisors.each { |d|
 
d < l1 && next
d > l2 && break
 
var t = n/d
 
next if (d%%10 && t%%10)
next if ("#{d}#{t}".sort != s)
 
take([d, t])
}
}
}
 
say "First 25 Vampire Numbers:"
 
with (1) { |i|
for (var n = 1; i <= 25; ++n) {
var fangs = is_vampire(n)
printf("%2d. %6s : %s\n", i++, n, fangs.join(' ')) if fangs
}
}
 
say "\nIndividual tests:"
 
[16758243290880, 24959017348650, 14593825548650].each { |n|
var fangs = is_vampire(n)
say "#{n}: #{fangs ? fangs.join(', ') : 'is not a vampire number'}"
}</syntaxhighlight>
{{out}}
<pre>
First 25 Vampire Numbers:
1. 1260 : [21, 60]
2. 1395 : [15, 93]
3. 1435 : [35, 41]
4. 1530 : [30, 51]
5. 1827 : [21, 87]
6. 2187 : [27, 81]
7. 6880 : [80, 86]
8. 102510 : [201, 510]
9. 104260 : [260, 401]
10. 105210 : [210, 501]
11. 105264 : [204, 516]
12. 105750 : [150, 705]
13. 108135 : [135, 801]
14. 110758 : [158, 701]
15. 115672 : [152, 761]
16. 116725 : [161, 725]
17. 117067 : [167, 701]
18. 118440 : [141, 840]
19. 120600 : [201, 600]
20. 123354 : [231, 534]
21. 124483 : [281, 443]
22. 125248 : [152, 824]
23. 125433 : [231, 543]
24. 125460 : [204, 615] [246, 510]
25. 125500 : [251, 500]
 
Individual tests:
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|Swift}}==
 
<syntaxhighlight lang="swift">import Foundation
 
func vampire<T>(n: T) -> [(T, T)] where T: BinaryInteger, T.Stride: SignedInteger {
let strN = String(n).sorted()
let fangLength = strN.count / 2
let start = T(pow(10, Double(fangLength - 1)))
let end = T(Double(n).squareRoot())
 
var fangs = [(T, T)]()
 
for i in start...end where n % i == 0 {
let quot = n / i
 
guard i % 10 != 0 || quot % 10 != 0 else {
continue
}
 
if "\(i)\(quot)".sorted() == strN {
fangs.append((i, quot))
}
}
 
return fangs
}
 
var count = 0
var i = 1.0
 
while count < 25 {
let start = Int(pow(10, i))
let end = start * 10
 
for num in start...end {
let fangs = vampire(n: num)
 
guard !fangs.isEmpty else { continue }
 
count += 1
 
print("\(num) is a vampire number with fangs: \(fangs)")
 
guard count != 25 else { break }
}
 
i += 2
}
 
for (vamp, fangs) in [16758243290880, 24959017348650, 14593825548650].lazy.map({ ($0, vampire(n: $0)) }) {
if fangs.isEmpty {
print("\(vamp) is not a vampire number")
} else {
print("\(vamp) is a vampire number with fangs: \(fangs)")
}
}</syntaxhighlight>
 
{{out}}
 
<pre>1260 is a vampire number with fangs: [(21, 60)]
1395 is a vampire number with fangs: [(15, 93)]
1435 is a vampire number with fangs: [(35, 41)]
1530 is a vampire number with fangs: [(30, 51)]
1827 is a vampire number with fangs: [(21, 87)]
2187 is a vampire number with fangs: [(27, 81)]
6880 is a vampire number with fangs: [(80, 86)]
102510 is a vampire number with fangs: [(201, 510)]
104260 is a vampire number with fangs: [(260, 401)]
105210 is a vampire number with fangs: [(210, 501)]
105264 is a vampire number with fangs: [(204, 516)]
105750 is a vampire number with fangs: [(150, 705)]
108135 is a vampire number with fangs: [(135, 801)]
110758 is a vampire number with fangs: [(158, 701)]
115672 is a vampire number with fangs: [(152, 761)]
116725 is a vampire number with fangs: [(161, 725)]
117067 is a vampire number with fangs: [(167, 701)]
118440 is a vampire number with fangs: [(141, 840)]
120600 is a vampire number with fangs: [(201, 600)]
123354 is a vampire number with fangs: [(231, 534)]
124483 is a vampire number with fangs: [(281, 443)]
125248 is a vampire number with fangs: [(152, 824)]
125433 is a vampire number with fangs: [(231, 543)]
125460 is a vampire number with fangs: [(204, 615), (246, 510)]
125500 is a vampire number with fangs: [(251, 500)]
16758243290880 is a vampire number with fangs: [(1982736, 8452080), (2123856, 7890480), (2751840, 6089832), (2817360, 5948208)]
24959017348650 is a vampire number with fangs: [(2947050, 8469153), (2949705, 8461530), (4125870, 6049395), (4129587, 6043950), (4230765, 5899410)]
14593825548650 is not a vampire number</pre>
 
=={{header|Tcl}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="tcl">proc factorPairs {n {from 2}} {
set result [list 1 $n]
if {$from<=1} {set from 2}
Line 3,050 ⟶ 4,052:
}
return $result
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl"># A nicer way to print the evidence of vampire-ness
proc printVampire {n pairs} {
set out "${n}:"
Line 3,076 ⟶ 4,078:
puts "$n is not a vampire number"
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,108 ⟶ 4,110:
24959017348650: [2947050, 8469153] [2949705, 8461530] [4125870, 6049395] [4129587, 6043950] [4230765, 5899410]
14593825548650 is not a vampire number
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import math
fn max(a u64, b u64) u64 {
if a > b {
return a
}
return b
}
fn min(a u64, b u64) u64 {
if a < b {
return a
}
return b
}
fn ndigits(xx u64) int {
mut n:=0
mut x := xx
for ; x > 0; x /= 10 {
n++
}
return n
}
fn dtally(xx u64) u64 {
mut t := u64(0)
mut x := xx
for ; x > 0; x /= 10 {
t += 1 << (x % 10 * 6)
}
return t
}
 
__global (
tens [20]u64
)
fn init() {
tens[0] = 1
for i := 1; i < 20; i++ {
tens[i] = tens[i-1] * 10
}
}
fn fangs(x u64) []u64 {
mut f := []u64{}
mut nd := ndigits(x)
if nd&1 == 1 {
return f
}
nd /= 2
lo := max(tens[nd-1], (x+tens[nd]-2)/(tens[nd]-1))
hi := min(x/lo, u64(math.sqrt(f64(x))))
t := dtally(x)
for a := lo; a <= hi; a++ {
b := x / a
if a*b == x &&
(a%10 > 0 || b%10 > 0) &&
t == dtally(a)+dtally(b) {
f << a
}
}
return f
}
fn show_fangs(x u64, f []u64) {
print(x)
if f.len > 1 {
println('')
}
for a in f {
println(" = $a × ${x/a}")
}
}
fn main() {
for x, n := u64(1), 0; n < 26; x++ {
f := fangs(x)
if f.len > 0 {
n++
print("${n:2}: ")
show_fangs(x, f)
}
}
println('')
for x in [u64(16758243290880), 24959017348650, 14593825548650] {
f := fangs(x)
if f.len > 0 {
show_fangs(x, f)
} else {
println("$x is not vampiric")
}
}
}</syntaxhighlight>
{{out}}
<pre> 1: 1260 = 21 × 60
2: 1395 = 15 × 93
3: 1435 = 35 × 41
4: 1530 = 30 × 51
5: 1827 = 21 × 87
6: 2187 = 27 × 81
7: 6880 = 80 × 86
8: 102510 = 201 × 510
9: 104260 = 260 × 401
10: 105210 = 210 × 501
11: 105264 = 204 × 516
12: 105750 = 150 × 705
13: 108135 = 135 × 801
14: 110758 = 158 × 701
15: 115672 = 152 × 761
16: 116725 = 161 × 725
17: 117067 = 167 × 701
18: 118440 = 141 × 840
19: 120600 = 201 × 600
20: 123354 = 231 × 534
21: 124483 = 281 × 443
22: 125248 = 152 × 824
23: 125433 = 231 × 543
24: 125460
= 204 × 615
= 246 × 510
25: 125500 = 251 × 500
26: 126027 = 201 × 627
 
16758243290880
= 1982736 × 8452080
= 2123856 × 7890480
= 2751840 × 6089832
= 2817360 × 5948208
24959017348650
= 2947050 × 8469153
= 2949705 × 8461530
= 4125870 × 6049395
= 4129587 × 6043950
= 4230765 × 5899410
14593825548650 is not vampiric
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var ndigits = Fn.new { |x|
var n = 0
while (x > 0) {
n = n + 1
x = (x/10).floor
}
return n
}
 
var dtally = Fn.new { |x|
var t = 0
while (x > 0) {
t = t + 2.pow((x%10) * 6)
x = (x/10).floor
}
return t
}
 
var tens = List.filled(15, 0)
 
var init = Fn.new {
tens[0] = 1
for (i in 1..14) tens[i] = tens[i-1] * 10
}
 
var fangs = Fn.new { |x|
var f = []
var nd = ndigits.call(x)
if ((nd&1) == 1) return f
nd = (nd/2).floor
var lo = tens[nd-1].max(((x + tens[nd] - 2) / (tens[nd] - 1)).floor)
var hi = (x/lo).floor.min(x.sqrt.floor)
var t = dtally.call(x)
var a = lo
while (a <= hi) {
var b = (x/a).floor
if (a*b == x && ((a%10) > 0 || (b%10) > 0) && t == dtally.call(a) + dtally.call(b)) {
f.add(a)
}
a = a + 1
}
return f
}
 
var showFangs = Fn.new { |x, f|
Fmt.write("$6d", x)
if (f.count > 1) System.print()
for (a in f) Fmt.print(" = $3d x $3d", a, (x/a).floor)
}
 
init.call()
var x = 1
var n = 0
while (n < 25) {
var f = fangs.call(x)
if (f.count > 0) {
n = n + 1
Fmt.write("$2d: ", n)
showFangs.call(x, f)
}
x = x + 1
}
System.print()
for (x in [16758243290880, 24959017348650, 14593825548650]) {
var f = fangs.call(x)
if (f.count > 0) {
showFangs.call(x, f)
} else {
Fmt.print("$d is not vampiric", x)
}
}</syntaxhighlight>
 
{{out}}
<pre>
1: 1260 = 21 x 60
2: 1395 = 15 x 93
3: 1435 = 35 x 41
4: 1530 = 30 x 51
5: 1827 = 21 x 87
6: 2187 = 27 x 81
7: 6880 = 80 x 86
8: 102510 = 201 x 510
9: 104260 = 260 x 401
10: 105210 = 210 x 501
11: 105264 = 204 x 516
12: 105750 = 150 x 705
13: 108135 = 135 x 801
14: 110758 = 158 x 701
15: 115672 = 152 x 761
16: 116725 = 161 x 725
17: 117067 = 167 x 701
18: 118440 = 141 x 840
19: 120600 = 201 x 600
20: 123354 = 231 x 534
21: 124483 = 281 x 443
22: 125248 = 152 x 824
23: 125433 = 231 x 543
24: 125460
= 204 x 615
= 246 x 510
25: 125500 = 251 x 500
 
16758243290880
= 1982736 x 8452080
= 2123856 x 7890480
= 2751840 x 6089832
= 2817360 x 5948208
24959017348650
= 2947050 x 8469153
= 2949705 x 8461530
= 4125870 x 6049395
= 4129587 x 6043950
= 4230765 x 5899410
14593825548650 is not vampiric
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn fangs(N){ //-->if Vampire number: (N,(a,b,c,...)), where a*x==N
var [const] tens=[0 .. 18].pump(List,(10.0).pow,"toInt");
 
Line 3,123 ⟶ 4,386:
});
fs and T(N,fs) or T;
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn vampiric(fangs,n=Void){
if(not fangs) return(n,"Not a Vampire number");
v:=fangs[0]; T(v,fangs[1].apply('wrap(n){T(n,v/n)})) }
Line 3,132 ⟶ 4,395:
 
(0).walker(*).tweak(fangs).filter(26)
.pump(Console.println,vampiric);</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits