Vampire number: Difference between revisions
m
→{{header|Wren}}: Minor tidy
No edit summary |
m (→{{header|Wren}}: Minor tidy) |
||
(45 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 decimal digits of the original number
::* together they consist of exactly the same decimal digits as the original number
::* at most one of them has a trailing zero
An example of a vampire number and its fangs: <code> 1260 : (21, 60) </code>
;Task:
# Print the first '''25'''
# Check if the following numbers are
<big><code> 16758243290880, 24959017348650, 14593825548650 </code></big>
Note that a
;See also:
* [http://www.numberphile.com/videos/vampire_numbers.html numberphile.com].
* [http://users.cybercity.dk/~dsl522332/math/vampires/
* [[oeis:A014575|
<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:
<
; (you can make it much faster by removing the informative tooltips)
Line 100 ⟶ 194:
}
Return SubStr(Output,3) ; 3 = 1 + length of "`n`t"
}</
{{out}}
<pre>1260: [21,60]
Line 143 ⟶ 237:
14593825548650:</pre>
=={{header|Bracmat}}==
<
= N len R fangsList
. !arg:@(?N:? [?len)
Line 204 ⟶ 299:
& (!count:<25|!bignums:%?i ?bignums)
)
);</
Output:
<pre>1260 (21,60)
Line 244 ⟶ 339:
=={{header|C}}==
<
#include <stdlib.h>
#include <stdint.h>
Line 325 ⟶ 420:
return 0;
}</
<pre> 1: 1260 = 21 x 60
2: 1395 = 15 x 93
Line 358 ⟶ 453:
=={{header|C sharp|C#}}==
{{trans|C}}
<
namespace RosettaVampireNumber
Line 447 ⟶ 542:
}
}
}</
{{out}}
<pre>
Line 482 ⟶ 577:
=={{header|C++}}==
<
#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 =
for ( long i = start ; i <
if ( number % i == 0 ) {
long quotient = number / i ;
if ( ( i % 10 == 0 ) && ( quotient % 10 == 0 ) )
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( ) ;
if ( vampireNumbersFound == 25 )
break ;
Line 542 ⟶ 637:
std::vector<long> testnumbers ;
testnumbers.push_back( 16758243290880 ) ;
testnumbers.push_back(
testnumbers.push_back( 14593825548650 ) ;
for ( std::vector<long>::const_iterator svl = testnumbers.begin( ) ;
Line 556 ⟶ 651:
}
return 0 ;
}</
{{out}}
<pre>
1 :1260 is a vampire number! These are the fangs:
[ 21 , 60 ]
_______________
[ 15 , 93 ]
_______________
[ 35 , 41
_______________
[ 30 , 51 ]
_______________
[ 21 , 87 ]
_______________
[ 27 , 81 ]
_______________
[ 80 , 86 ]
_______________
8 :102510 is a vampire number! These are the fangs:
[ 201 , 510 ]
_______________
[ 260 , 401
_______________
[ 210 , 501 ]
_______________
[ 204 , 516 ]
_______________
[ 150 , 705 ]
_______________
[ 135 , 801 ]
_______________
[ 158 , 701 ]
_______________
[ 152 , 761 ]
_______________
[ 161 , 725 ]
_______________
[ 167 , 701 ]
_______________
[ 141 , 840 ]
_______________
[ 201 , 600 ]
_______________
20 :123354 is a vampire number! These are the fangs:
[ 231 , 534 ]
_______________
[ 281 , 443
_______________
[ 152 , 824 ]
_______________
[ 231 , 543 ]
_______________
[ 204 , 615 ][ 246 , 510 ]
_______________
[ 251 , 500 ]
_______________
16758243290880 is a vampire number! The fangs:
[ 1982736 , 8452080 ][ 2123856 , 7890480 ][ 2751840 , 6089832 ][ 2817360 , 5948208 ]
[
14593825548650 is not a vampire number!
</pre>
=={{header|Clojure}}==
<
(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."))))</
=={{header|Common Lisp}}==
<
"Is the lowest digit of `number' a 0"
(zerop (rem number 10)))
Line 727 ⟶ 823:
(when fangs
(print-vampire candidate fangs))))
</syntaxhighlight>
{{out}}
Line 768 ⟶ 864:
{{trans|Clojure}}
(Runtime about 1.34 seconds with dmd.)
<
auto fangs(in long n) pure nothrow @safe {
Line 786 ⟶ 882:
14593825548650].map!fangs))
writefln("%d: (%(%(%s %)) (%))", v[]);
}</
{{out}}
<pre>1260: (21 60)
Line 820 ⟶ 916:
{{trans|C}}
(Runtime about 0.27 seconds with dmd, about 0.25 seconds with ldc2 compilers.)
<
T[N] pows(T, size_t N)() pure nothrow @safe @nogc {
Line 911 ⟶ 1,007:
showFangs(bi, fs);
}
}</
{{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">
class
APPLICATION
Line 1,055 ⟶ 1,206:
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,098 ⟶ 1,249:
=={{header|Elixir}}==
{{works with|Elixir|1.1+}}
<
def factor_pairs(n) do
first = trunc(n / :math.pow(10, div(char_len(n), 2)))
Line 1,139 ⟶ 1,290:
end
Vampire.task</
{{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}}==
<
'FreeBASIC version 24. Windows
'Vampire.bas
Line 1,279 ⟶ 1,601:
Print "Completed in ";
Print t2-t1;" Seconds"
Sleep</
{{out}}
<pre>First 28 Vampire numbers
Line 1,330 ⟶ 1,652:
Completed in 1.286374813709699 Seconds</pre>
=={{header|Go}}==
{{trans|C}}
<
import (
Line 1,422 ⟶ 1,745:
}
}
}</
{{out}}
<pre>
Line 1,470 ⟶ 1,793:
=={{header|Haskell}}==
<
import Control.Arrow ((&&&))
-- VAMPIRE NUMBERS ------------------------------------------------------------
vampires :: [Int]
vampires = filter (
fangs :: Int -> [(Int, Int)]
Line 1,518 ⟶ 1,841:
mapM
(print . ((,) <*>) fangs)
(take 25 vampires ++ [16758243290880, 24959017348650, 14593825548650])</
{{Out}}
<pre>
Line 1,554 ⟶ 1,877:
The following works in both languages.
<
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</
Output:
Line 1,617 ⟶ 1,940:
=={{header|J}}==
<syntaxhighlight 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>
=={{header|Java}}==
{{works with|Java|1.5+}}
<
import java.util.HashSet;
Line 1,709 ⟶ 2,032:
}
}
}</
Output:
<pre>1260: [21, 60]
Line 1,749 ⟶ 2,072:
===Alternative version===
<
public static void main(String args[]) {
Line 1,818 ⟶ 2,141:
return total;
}
}</
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">
function divisors{T<:Integer}(n::T)
!isprime(n) || return [one(T), n]
Line 1,890 ⟶ 2,295:
return (isvampire, fangs)
end
</syntaxhighlight>
'''Main'''
<syntaxhighlight lang="julia">using Printf
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>
{{out}}
Line 1,978 ⟶ 2,384:
=={{header|Kotlin}}==
<
data class Fangs(val fang1: Long = 0L, val fang2: Long = 0L)
Line 2,073 ⟶ 2,479:
}
}
}</
{{out}}
Line 2,109 ⟶ 2,515:
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
VampireQ[num_Integer] := Module[{poss, divs},
divs = Select[Divisors[num], # <= Sqrt[num] &];
Line 2,136 ⟶ 2,542:
False
]
]</
Testing out the function:
<
Reap[VampireQ[#]] & /@ {16758243290880, 24959017348650, 14593825548650} // Grid</
{{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}}==
{{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])))</
Output:
<pre>1260 21 60
Line 2,217 ⟶ 2,708:
=={{header|Perl}}==
<
use warnings;
use strict;
Line 2,251 ⟶ 2,742:
}
say join ' ', $_, map "[@$_]", fangs($_) for 16758243290880, 24959017348650, 14593825548650;</
A faster version, using the `divisors()` function from the <code>ntheory</code> library.
{{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));
my @fangs;
foreach my $d (divisors($n)) {
$d < $l1 and next;
$d > $l2 and last;
my $t = $n / $d;
next if ($d % 10 == 0 and $t % 10 == 0);
next if (join('', sort split(//, "$d$t")) ne $s);
push @fangs, [$d, $t];
}
return @fangs;
}
print "First 25 Vampire Numbers:\n";
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]
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|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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}}==
<
DisableDebugger
Line 2,391 ⟶ 2,969:
EndIf
Next
Return</
{{out}}
<pre>The first 25 Vampire numbers...
Line 2,441 ⟶ 3,019:
=={{header|Python}}==
This routine finds ''all'' the fangs for a number.
<
import math
Line 2,517 ⟶ 3,095:
for n in (16758243290880, 24959017348650, 14593825548650):
fangpairs = vampire(n)
print('%i: %r' % (n, fangpairs))</
{{out}}
<pre>First 25 vampire numbers
Line 2,553 ⟶ 3,131:
This alternative solution is not fast but it's short:
{{trans|Clojure}}
<
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."</
{{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?":
<
;; 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))</
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: if the argument is negative, its absolute value is used to test if <u>that</u> single number is vampiric.
<
!.0= 1260; !.1= 11453481; !.2= 115672; !.3= 124483; !.4= 105264
!.5= 1395; !.6=
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;
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;
if f=='' then say
else say
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
vampire: procedure;
do k=1 for L; _= substr(
top= left( reverse(bot), w)
bot= left(bot, w)
inc= x // 2 +
beg= max(bot, 10 ** (w-1) )
if inc==2 then if beg//2==0 then
do d=beg
q= x
if length(q)
if q*d//9
parse var q '' -1 _
if _== 0 then if right(d, 1) == 0
dq= d
t= x;
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.*/</syntaxhighlight>
{{out|output|text= when using the default input:}}
<pre>
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
vampire number
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>
<pre>
16758243290880 is a vampire number, fangs= [1982736∙8452080] [2123856∙7890480] [2751840∙6089832] [2817360∙5948208]
</pre>
<pre>
24959017348650 is a vampire number, fangs= [2947050∙8469153] [2949705∙8461530] [4125870∙6049395] [4129587∙6043950] [4230765∙5899410]
</pre>
<pre>
14593825548650 isn't a vampire number.
Line 2,762 ⟶ 3,415:
=={{header|Ring}}==
<
# Project : Vampire number
for p = 10 to 127000
Line 2,845 ⟶ 3,495:
next
return alist
</syntaxhighlight>
Output:
<pre>
Line 2,871 ⟶ 3,521:
125433: [231,543]
125460: [204,615]
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}}==
<
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</
{{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}}
<
import math._
import scala.collection.mutable.ListBuffer
Line 2,991 ⟶ 3,830:
println("\n"+"elapsed time: "+et+" seconds")
}</
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}}
<
set result [list 1 $n]
if {$from<=1} {set from 2}
Line 3,050 ⟶ 4,052:
}
return $result
}</
Demonstrating:
<
proc printVampire {n pairs} {
set out "${n}:"
Line 3,076 ⟶ 4,078:
puts "$n is not a vampire number"
}
}</
{{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}}==
<
var [const] tens=[0 .. 18].pump(List,(10.0).pow,"toInt");
Line 3,123 ⟶ 4,386:
});
fs and T(N,fs) or T;
}</
<
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);</
{{out}}
<pre>
|