Vampire number: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(25 intermediate revisions by 16 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 556 ⟶ 651:
}
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 642 ⟶ 737:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn factor-pairs [n]
(for [x (range 2 (Math/sqrt n))
:when (zero? (mod n x))]
Line 664 ⟶ 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 728 ⟶ 823:
(when fangs
(print-vampire candidate fangs))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 769 ⟶ 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 787 ⟶ 882:
14593825548650].map!fangs))
writefln("%d: (%(%(%s %)) (%))", v[]);
}</langsyntaxhighlight>
{{out}}
<pre>1260: (21 60)
Line 821 ⟶ 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 912 ⟶ 1,007:
showFangs(bi, fs);
}
}</langsyntaxhighlight>
{{out}}
<pre> 2: 1395 = 15 x 93
Line 942 ⟶ 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,056 ⟶ 1,206:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,099 ⟶ 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,140 ⟶ 1,290:
end
 
Vampire.task</langsyntaxhighlight>
 
{{out}}
Line 1,176 ⟶ 1,326:
 
=={{header|Factor}}==
<langsyntaxhighlight 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 ;
Line 1,219 ⟶ 1,369:
: main ( -- ) part1 part2 ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,250 ⟶ 1,400:
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,357 ⟶ 1,601:
Print "Completed in ";
Print t2-t1;" Seconds"
Sleep</langsyntaxhighlight>
{{out}}
<pre>First 28 Vampire numbers
Line 1,408 ⟶ 1,652:
 
Completed in 1.286374813709699 Seconds</pre>
 
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,500 ⟶ 1,745:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,548 ⟶ 1,793:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List (sort)
import Control.Arrow ((&&&))
 
Line 1,596 ⟶ 1,841:
mapM
(print . ((,) <*>) fangs)
(take 25 vampires ++ [16758243290880, 24959017348650, 14593825548650])</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,632 ⟶ 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,657 ⟶ 1,902:
every (s1 := "", c := !cset(s)) do every find(c, s) do s1 ||:= c
return s1
end</langsyntaxhighlight>
 
Output:
Line 1,695 ⟶ 1,940:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
Filter=: (#~`)(`:6)
odd =: 2&|
Line 1,736 ⟶ 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,787 ⟶ 2,032:
}
}
}</langsyntaxhighlight>
Output:
<pre>1260: [21, 60]
Line 1,827 ⟶ 2,072:
 
===Alternative version===
<langsyntaxhighlight lang="java">public class VampireNumber {
 
public static void main(String args[]) {
Line 1,896 ⟶ 2,141:
return total;
}
}</langsyntaxhighlight>
 
Output:
Line 1,930 ⟶ 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,968 ⟶ 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 2,008 ⟶ 2,336:
end
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,056 ⟶ 2,384:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1
 
data class Fangs(val fang1: Long = 0L, val fang2: Long = 0L)
Line 2,151 ⟶ 2,479:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,187 ⟶ 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,214 ⟶ 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,249 ⟶ 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}}==
Line 2,254 ⟶ 2,664:
{{Lines too long|PARI/GP}}
 
<langsyntaxhighlight 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,298 ⟶ 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,332 ⟶ 2,742:
}
 
say join ' ', $_, map "[@$_]", fangs($_) for 16758243290880, 24959017348650, 14593825548650;</langsyntaxhighlight>
 
A faster version, using the `divisors()` function from the <code>ntheory</code> library.
{{trans|Sidef}}
{{libheader|ntheory}}
<lang perl>use ntheory qw(sqrtint logint divisors);
<syntaxhighlight lang="perl">use ntheory qw(sqrtint logint divisors);
 
sub is_vampire {
Line 2,380 ⟶ 2,791:
print("$n: ", (@fangs ? join(' ', map { "[@$_]" } @fangs)
: "is not a vampire number"), "\n");
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,414 ⟶ 2,825:
24959017348650: [2947050 8469153] [2949705 8461530] [4125870 6049395] [4129587 6043950] [4230765 5899410]
14593825548650: is not a vampire number
</pre>
 
=={{header|Perl 6}}==
 
<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;
}
 
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;
}
 
say "\nFirst 25 Vampire Numbers:\n";
 
.say for @vampires[^25];
 
say "\nIndividual tests:\n";
 
for 16758243290880, 24959017348650, 14593825548650 {
print "$_: ";
my @fangs = is_vampire($_);
if @fangs.elems {
say @fangs.join(', ');
} else {
say 'is not a vampire number.';
}
}
 
sub vfactors (Int $n) {
map { $_ if $n %% $_ }, 10**$n.sqrt.log(10).floor .. $n.sqrt.ceiling;
}</lang>
 
<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|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function vampire(atom v)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence res = {}
<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>
if v>=0 then
<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>
string vs = sprintf("%d",v)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
if mod(length(vs),2)=0 then -- even length
<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>
vs = sort(vs)
<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>
for i=power(10,length(vs)/2-1) to floor(sqrt(v)) do
<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>
if remainder(v,i)=0 then
<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>
integer i2 = v/i
<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>
string si = sprintf("%d",i),
<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>
s2 = sprintf("%d",i2)
<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>
if (si[$]!='0' or s2[$]!='0')
<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>
and sort(si&s2)=vs then
<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>
res = append(res,{i,i2})
<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>
end if
<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>
end if
<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>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
integer found = 0
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
atom i = 0
sequence res
<span style="color: #004080;">integer</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
puts(1,"The first 26 vampire numbers and their fangs:\n")
<span style="color: #004080;">atom</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
while found<26 do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span>
res = vampire(i)
<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>
if length(res) then
<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>
found += 1
<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>
printf(1,"%d: %d: %s\n",{found,i,sprint(res)})
<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>
end if
<span style="color: #000000;">found</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
i += 1
<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>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
puts(1,"\n")
<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>
constant tests = {16758243290880,
<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>
24959017348650,
14593825548650}
<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>
for t=1 to length(tests) do
<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>
i = tests[t]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
res = vampire(i)
<!--</syntaxhighlight>-->
printf(1,"%d: %s\n",{i,iff(res={}?"not a vampire number":sprint(res))})
end for</lang>
{{out}}
<pre>
Line 2,574 ⟶ 2,908:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">EnableExplicit
DisableDebugger
 
Line 2,635 ⟶ 2,969:
EndIf
Next
Return</langsyntaxhighlight>
{{out}}
<pre>The first 25 Vampire numbers...
Line 2,685 ⟶ 3,019:
=={{header|Python}}==
This routine finds ''all'' the fangs for a number.
<langsyntaxhighlight lang="python">from __future__ import division
 
import math
Line 2,761 ⟶ 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,797 ⟶ 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,819 ⟶ 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,854 ⟶ 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,887 ⟶ 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,919 ⟶ 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. */
parse arg N . /*obtain optional argument from the CL.*/
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; !.7= 1827; !.8= 110758; !.9= 156289 /* " " " */
#L= 0length(N); L= length(N); aN= abs(N) /*num.L: length of vampireN; numbers found,aN: soabsolute farvalue*/
numeric digits max(9, length(aN) ) /*be able to handle ginormus numbers. */
@vamp# = right('vampire0 number', 20) /*literal#: used whencount showing aof 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 to even length*/
Line 2,938 ⟶ 3,338:
if f=='' then iterate /*Are fangs null? Yes, not vampire. */
#= # + 1 /*bump the vampire count, Vlad. */
say @vampright('vampire number', 20) right(#, L) "is: " right(j, 9)', fangs=' f
end /*j*/ /* [↑] process a range of numbers. */
else do; f= vampire(aN) /* [↓] process a number; obtain fangs.*/
if f=='' then say aN " isn't a vampire number."
else say 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 len of X*/
if L//2 then return '' /*is L odd? Then ¬vampire.*/
do k=1 for L; _= substr(x, k, 1); $._= $._ || _
end /*k*/
w= L % 2 /*%: is REXX's integer ÷ */
do m=0 for 10; bot= bot || $.m
end /*m*/
top= left( reverse(bot), w)
bot= left(bot, w) /*determine limits of search*/
inc= x // 2 + 1 /*X is odd? INC=2. No? INC=1*/
beg= max(bot, 10 ** (w-1) ) ) /*calculate where to startbegin.*/
if inc==2 then if beg//2==0 then beg= beg + 1 /*possibly adjust the startbegin.*/
/* [↑] odd BEG if odd INC*/
do d=beg to min(top, 10**w - 1) by inc /*use smart start BEG, endEND, incINC. */
if x // d \==0 then iterate /*X not ÷ by D? Then skip,*/
q= x % d; if d>q then iterate /*is D > Q Then skip. " " */
if length(q) \== w then iterate /*Len of Q ¬= W? Then skip.*/
if q*d//9 \== (q+d)//9 then iterate /*modulo 9 congruence test. */
if length(q) \==w then iterate /*Len of Q ¬= W? Then skip.*/
parse var q '' -1 _ /*get last decimal dig. of Q*/
if _== 0 then if right(d, 1) == 0 then iterate
dq= d || q
t= x; do i=1 for L; p= pos( substr(dq, i, 1), t)
if p==0 then iterate d; t= delstr(t, p, 1)
end /*i*/
a= a '['d"∙"q'] ' /*construct formatted fangs.*/
end /*d*/ /* [↑] ∙ is a round bullet*/
return a /*return formatted fangs.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 3,005 ⟶ 3,405:
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 3,015 ⟶ 3,415:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Vampire number
 
Line 3,095 ⟶ 3,495:
next
return alist
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,124 ⟶ 3,524:
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 3,157 ⟶ 3,620:
puts "#{n}:\t#{vf}"
end
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,189 ⟶ 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 3,241 ⟶ 3,830:
 
println("\n"+"elapsed time: "+et+" seconds")
}</langsyntaxhighlight>
Output:
<pre style="height: 30ex; overflow: scroll">1: 1260 = 21 x 60
Line 3,276 ⟶ 3,865:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_vampire (n) {
return [] if n.ilog10.is_even
 
Line 3,314 ⟶ 3,903:
var fangs = is_vampire(n)
say "#{n}: #{fangs ? fangs.join(', ') : 'is not a vampire number'}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,352 ⟶ 3,941:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
func vampire<T>(n: T) -> [(T, T)] where T: BinaryInteger, T.Stride: SignedInteger {
Line 3,405 ⟶ 3,994:
print("\(vamp) is a vampire number with fangs: \(fangs)")
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,440 ⟶ 4,029:
=={{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,463 ⟶ 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,489 ⟶ 4,078:
puts "$n is not a vampire number"
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,521 ⟶ 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,536 ⟶ 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,545 ⟶ 4,395:
 
(0).walker(*).tweak(fangs).filter(26)
.pump(Console.println,vampiric);</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits