Vampire number: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 30:
{{trans|Nim}}
<
F isOdd(n)
Line 83:
print(n‘ is not vampiric.’)
E
print(n‘ = ’fangList.map(it -> ‘#. x #.’.format(it[0], it[1])).join(‘ = ’))</
{{out}}
Line 121:
=={{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 194:
}
Return SubStr(Output,3) ; 3 = 1 + length of "`n`t"
}</
{{out}}
<pre>1260: [21,60]
Line 239:
=={{header|Bracmat}}==
<
= N len R fangsList
. !arg:@(?N:? [?len)
Line 299:
& (!count:<25|!bignums:%?i ?bignums)
)
);</
Output:
<pre>1260 (21,60)
Line 339:
=={{header|C}}==
<
#include <stdlib.h>
#include <stdint.h>
Line 420:
return 0;
}</
<pre> 1: 1260 = 21 x 60
2: 1395 = 15 x 93
Line 453:
=={{header|C sharp|C#}}==
{{trans|C}}
<
namespace RosettaVampireNumber
Line 542:
}
}
}</
{{out}}
<pre>
Line 577:
=={{header|C++}}==
<
#include <utility>
#include <algorithm>
Line 651:
}
return 0 ;
}</
{{out}}
<pre>
Line 737:
=={{header|Clojure}}==
<
(for [x (range 2 (Math/sqrt n))
:when (zero? (mod n x))]
Line 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 823:
(when fangs
(print-vampire candidate fangs))))
</syntaxhighlight>
{{out}}
Line 864:
{{trans|Clojure}}
(Runtime about 1.34 seconds with dmd.)
<
auto fangs(in long n) pure nothrow @safe {
Line 882:
14593825548650].map!fangs))
writefln("%d: (%(%(%s %)) (%))", v[]);
}</
{{out}}
<pre>1260: (21 60)
Line 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 1,007:
showFangs(bi, fs);
}
}</
{{out}}
<pre> 2: 1395 = 15 x 93
Line 1,039:
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,151:
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,194:
=={{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,235:
end
Vampire.task</
{{out}}
Line 1,271:
=={{header|Factor}}==
<
math.combinatorics math.functions math.primes.factors math.statistics
math.text.utils prettyprint sequences sets ;
Line 1,314:
: main ( -- ) part1 part2 ;
MAIN: main</
{{out}}
<pre>
Line 1,347:
=={{header|FreeBASIC}}==
<
'FreeBASIC version 24. Windows
'Vampire.bas
Line 1,452:
Print "Completed in ";
Print t2-t1;" Seconds"
Sleep</
{{out}}
<pre>First 28 Vampire numbers
Line 1,506:
=={{header|Go}}==
{{trans|C}}
<
import (
Line 1,596:
}
}
}</
{{out}}
<pre>
Line 1,644:
=={{header|Haskell}}==
<
import Control.Arrow ((&&&))
Line 1,692:
mapM
(print . ((,) <*>) fangs)
(take 25 vampires ++ [16758243290880, 24959017348650, 14593825548650])</
{{Out}}
<pre>
Line 1,728:
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,753:
every (s1 := "", c := !cset(s)) do every find(c, s) do s1 ||:= c
return s1
end</
Output:
Line 1,791:
=={{header|J}}==
<syntaxhighlight lang="j">
Filter=: (#~`)(`:6)
odd =: 2&|
Line 1,832:
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,883:
}
}
}</
Output:
<pre>1260: [21, 60]
Line 1,923:
===Alternative version===
<
public static void main(String args[]) {
Line 1,992:
return total;
}
}</
Output:
Line 2,032:
'''Works with gojq, the Go implementation of jq'''
<
# To take advantage of gojq's arbitrary-precision integer arithmetic:
Line 2,075:
task1(25),
"",
task2</
{{out}}
<pre>
Line 2,111:
=={{header|Julia}}==
'''Functions'''
<syntaxhighlight lang="julia">
function divisors{T<:Integer}(n::T)
!isprime(n) || return [one(T), n]
Line 2,146:
return (isvampire, fangs)
end
</syntaxhighlight>
'''Main'''
<
function showvampire{T<:Integer}(i::T, n::T, fangs::Array{T,2})
Line 2,187:
end
end
</syntaxhighlight>
{{out}}
Line 2,235:
=={{header|Kotlin}}==
<
data class Fangs(val fang1: Long = 0L, val fang2: Long = 0L)
Line 2,330:
}
}
}</
{{out}}
Line 2,367:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
VampireQ[num_Integer] := Module[{poss, divs},
divs = Select[Divisors[num], # <= Sqrt[num] &];
Line 2,393:
False
]
]</
Testing out the function:
<
Reap[VampireQ[#]] & /@ {16758243290880, 24959017348650, 14593825548650} // Grid</
{{out}}
<pre>
Line 2,430:
=={{header|Nim}}==
<
const Pow10 = collect(newSeq, for n in 0..18: 10 ^ n)
Line 2,477:
echo &"{n} is not vampiric."
else:
echo &"{n} = ", fangList.mapIt(&"{it[0]} × {it[1]}").join(" = ")</
{{out}}
Line 2,515:
{{Lines too long|PARI/GP}}
<
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,561:
The trailing zeros condition is first triggered when searching for the 26th vampire number.
<
use warnings;
use strict;
Line 2,593:
}
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}}
<
sub is_vampire {
Line 2,642:
print("$n: ", (@fangs ? join(' ', map { "[@$_]" } @fangs)
: "is not a vampire number"), "\n");
}</
{{out}}
<pre>
Line 2,679:
=={{header|Phix}}==
<!--<
<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>
Line 2,722:
<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>
<!--</
{{out}}
<pre>
Line 2,759:
=={{header|PureBasic}}==
<
DisableDebugger
Line 2,820:
EndIf
Next
Return</
{{out}}
<pre>The first 25 Vampire numbers...
Line 2,870:
=={{header|Python}}==
This routine finds ''all'' the fangs for a number.
<
import math
Line 2,946:
for n in (16758243290880, 24959017348650, 14593825548650):
fangpairs = vampire(n)
print('%i: %r' % (n, fangpairs))</
{{out}}
<pre>First 25 vampire numbers
Line 2,982:
This alternative solution is not fast but it's short:
{{trans|Clojure}}
<
from itertools import imap, ifilter, islice, count
Line 3,004:
for n in [16758243290880, 24959017348650, 14593825548650]:
print vampiricQ(n) or str(n) + " is not vampiric."</
{{out}}
<pre>(1260, [(21, 60)])
Line 3,039:
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 3,072:
(displayln (vampire?-and-fangs 16758243290880))
(displayln (vampire?-and-fangs 24959017348650))
(displayln (vampire?-and-fangs 14593825548650))</
Output:
<pre>First 25 vampire numbers:
Line 3,108:
(formerly Perl 6)
<syntaxhighlight lang="raku"
my $digits = $num.comb.sort;
my @fangs;
Line 3,135:
.say for (16758243290880, 24959017348650, 14593825548650).hyper(:1batch).map: {
"$_: " ~ (is_vampire($_).join(', ') || 'is not a vampire number.')
}</
<pre>First 25 Vampire Numbers:
Line 3,173:
=={{header|REXX}}==
Note: if the argument is negative, its absolute value is used to test if <u>that</u> single number is vampiric.
<
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 25 /*Not specified? Then use the default.*/
Line 3,223:
a= a '['d"∙"q'] ' /*construct formatted fangs.*/
end /*d*/ /* [↑] ∙ is a round bullet*/
return a /*return formatted fangs.*/</
{{out|output|text= when using the default input:}}
<pre>
Line 3,266:
=={{header|Ring}}==
<
# Project : Vampire number
Line 3,346:
next
return alist
</syntaxhighlight>
Output:
<pre>
Line 3,378:
=={{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 3,408:
puts "#{n}:\t#{vf}"
end
end</
{{out}}
<pre>
Line 3,442:
=={{header|Rust}}==
<
static TENS: [u64; 20] = [
Line 3,565:
assert_eq!(fangs(14593825548650), vec![]);
}</
=={{header|Scala}}==
{{works with|Scala|2.9.1}}
<
import math._
import scala.collection.mutable.ListBuffer
Line 3,618:
println("\n"+"elapsed time: "+et+" seconds")
}</
Output:
<pre style="height: 30ex; overflow: scroll">1: 1260 = 21 x 60
Line 3,653:
=={{header|Sidef}}==
<
return [] if n.ilog10.is_even
Line 3,691:
var fangs = is_vampire(n)
say "#{n}: #{fangs ? fangs.join(', ') : 'is not a vampire number'}"
}</
{{out}}
<pre>
Line 3,729:
=={{header|Swift}}==
<
func vampire<T>(n: T) -> [(T, T)] where T: BinaryInteger, T.Stride: SignedInteger {
Line 3,782:
print("\(vamp) is a vampire number with fangs: \(fangs)")
}
}</
{{out}}
Line 3,817:
=={{header|Tcl}}==
{{trans|Ruby}}
<
set result [list 1 $n]
if {$from<=1} {set from 2}
Line 3,840:
}
return $result
}</
Demonstrating:
<
proc printVampire {n pairs} {
set out "${n}:"
Line 3,866:
puts "$n is not a vampire number"
}
}</
{{out}}
<pre>
Line 3,902:
=={{header|Vlang}}==
{{trans|Go}}
<
fn max(a u64, b u64) u64 {
if a > b {
Line 3,995:
}
}
}</
{{out}}
<pre> 1: 1260 = 21 × 60
Line 4,043:
{{trans|Go}}
{{libheader|Wren-fmt}}
<
var ndigits = Fn.new { |x|
Line 4,115:
Fmt.print("$d is not vampiric", x)
}
}</
{{out}}
Line 4,162:
=={{header|zkl}}==
<
var [const] tens=[0 .. 18].pump(List,(10.0).pow,"toInt");
Line 4,174:
});
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 4,183:
(0).walker(*).tweak(fangs).filter(26)
.pump(Console.println,vampiric);</
{{out}}
<pre>
|