Vampire number: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 30:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">-V Pow10 = (0..18).map(n -> Int64(10) ^ n)
 
F isOdd(n)
Line 83:
print(n‘ is not vampiric.’)
E
print(n‘ = ’fangList.map(it -> ‘#. x #.’.format(it[0], it[1])).join(‘ = ’))</langsyntaxhighlight>
 
{{out}}
Line 121:
=={{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 194:
}
Return SubStr(Output,3) ; 3 = 1 + length of "`n`t"
}</langsyntaxhighlight>
{{out}}
<pre>1260: [21,60]
Line 239:
 
=={{header|Bracmat}}==
<langsyntaxhighlight Bracmatlang="bracmat">( ( vampire
= N len R fangsList
. !arg:@(?N:? [?len)
Line 299:
& (!count:<25|!bignums:%?i ?bignums)
)
);</langsyntaxhighlight>
Output:
<pre>1260 (21,60)
Line 339:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Line 420:
 
return 0;
}</langsyntaxhighlight>
<pre> 1: 1260 = 21 x 60
2: 1395 = 15 x 93
Line 453:
=={{header|C sharp|C#}}==
{{trans|C}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace RosettaVampireNumber
Line 542:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 577:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <vector>
#include <utility>
#include <algorithm>
Line 651:
}
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 737:
 
=={{header|Clojure}}==
<langsyntaxhighlight Clojurelang="clojure">(defn factor-pairs [n]
(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."))))</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
 
<langsyntaxhighlight lang="lisp">(defun trailing-zerop (number)
"Is the lowest digit of `number' a 0"
(zerop (rem number 10)))
Line 823:
(when fangs
(print-vampire candidate fangs))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 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 882:
14593825548650].map!fangs))
writefln("%d: (%(%(%s %)) (%))", v[]);
}</langsyntaxhighlight>
{{out}}
<pre>1260: (21 60)
Line 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 1,007:
showFangs(bi, fs);
}
}</langsyntaxhighlight>
{{out}}
<pre> 2: 1395 = 15 x 93
Line 1,039:
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,151:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,194:
=={{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,235:
end
 
Vampire.task</langsyntaxhighlight>
 
{{out}}
Line 1,271:
 
=={{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,314:
: main ( -- ) part1 part2 ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,347:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">'Vampire numbers.
'FreeBASIC version 24. Windows
'Vampire.bas
Line 1,452:
Print "Completed in ";
Print t2-t1;" Seconds"
Sleep</langsyntaxhighlight>
{{out}}
<pre>First 28 Vampire numbers
Line 1,506:
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,596:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,644:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List (sort)
import Control.Arrow ((&&&))
 
Line 1,692:
mapM
(print . ((,) <*>) fangs)
(take 25 vampires ++ [16758243290880, 24959017348650, 14593825548650])</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,728:
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,753:
every (s1 := "", c := !cset(s)) do every find(c, s) do s1 ||:= c
return s1
end</langsyntaxhighlight>
 
Output:
Line 1,791:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<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>
</lang>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.HashSet;
 
Line 1,883:
}
}
}</langsyntaxhighlight>
Output:
<pre>1260: [21, 60]
Line 1,923:
 
===Alternative version===
<langsyntaxhighlight lang="java">public class VampireNumber {
 
public static void main(String args[]) {
Line 1,992:
return total;
}
}</langsyntaxhighlight>
 
Output:
Line 2,032:
'''Works with gojq, the Go implementation of jq'''
 
<langsyntaxhighlight lang="jq">def count(s): reduce s as $x (0; .+1);
 
# To take advantage of gojq's arbitrary-precision integer arithmetic:
Line 2,075:
task1(25),
"",
task2</langsyntaxhighlight>
{{out}}
<pre>
Line 2,111:
=={{header|Julia}}==
'''Functions'''
<syntaxhighlight lang="julia">
<lang Julia>
function divisors{T<:Integer}(n::T)
!isprime(n) || return [one(T), n]
Line 2,146:
return (isvampire, fangs)
end
</syntaxhighlight>
</lang>
 
'''Main'''
<langsyntaxhighlight Julialang="julia">using Printf
 
function showvampire{T<:Integer}(i::T, n::T, fangs::Array{T,2})
Line 2,187:
end
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,235:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1
 
data class Fangs(val fang1: Long = 0L, val fang2: Long = 0L)
Line 2,330:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,367:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[VampireQ]
VampireQ[num_Integer] := Module[{poss, divs},
divs = Select[Divisors[num], # <= Sqrt[num] &];
Line 2,393:
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,430:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, math, sequtils, strformat, strutils, sugar
 
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(" = ")</langsyntaxhighlight>
 
{{out}}
Line 2,515:
{{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,561:
The trailing zeros condition is first triggered when searching for the 26th vampire number.
 
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 2,593:
}
 
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}}
<langsyntaxhighlight lang="perl">use ntheory qw(sqrtint logint divisors);
 
sub is_vampire {
Line 2,642:
print("$n: ", (@fangs ? join(' ', map { "[@$_]" } @fangs)
: "is not a vampire number"), "\n");
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,679:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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>
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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,759:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">EnableExplicit
DisableDebugger
 
Line 2,820:
EndIf
Next
Return</langsyntaxhighlight>
{{out}}
<pre>The first 25 Vampire numbers...
Line 2,870:
=={{header|Python}}==
This routine finds ''all'' the fangs for a number.
<langsyntaxhighlight lang="python">from __future__ import division
 
import math
Line 2,946:
for n in (16758243290880, 24959017348650, 14593825548650):
fangpairs = vampire(n)
print('%i: %r' % (n, fangpairs))</langsyntaxhighlight>
{{out}}
<pre>First 25 vampire numbers
Line 2,982:
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 3,004:
 
for n in [16758243290880, 24959017348650, 14593825548650]:
print vampiricQ(n) or str(n) + " is not vampiric."</langsyntaxhighlight>
{{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?":
<langsyntaxhighlight lang="racket">#lang racket
 
;; chock full of fun... including divisors
Line 3,072:
(displayln (vampire?-and-fangs 16758243290880))
(displayln (vampire?-and-fangs 24959017348650))
(displayln (vampire?-and-fangs 14593825548650))</langsyntaxhighlight>
Output:
<pre>First 25 vampire numbers:
Line 3,108:
(formerly Perl 6)
 
<syntaxhighlight lang="raku" perl6line>sub is_vampire (Int $num) {
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.')
}</langsyntaxhighlight>
 
<pre>First 25 Vampire Numbers:
Line 3,173:
=={{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.*/
Line 3,223:
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,266:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Vampire number
 
Line 3,346:
next
return alist
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,378:
 
=={{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,408:
puts "#{n}:\t#{vf}"
end
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,442:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">use std::cmp::{max, min};
 
static TENS: [u64; 20] = [
Line 3,565:
 
assert_eq!(fangs(14593825548650), vec![]);
}</langsyntaxhighlight>
 
=={{header|Scala}}==
{{works with|Scala|2.9.1}}
<langsyntaxhighlight Scalalang="scala">import Stream._
import math._
import scala.collection.mutable.ListBuffer
Line 3,618:
 
println("\n"+"elapsed time: "+et+" seconds")
}</langsyntaxhighlight>
Output:
<pre style="height: 30ex; overflow: scroll">1: 1260 = 21 x 60
Line 3,653:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_vampire (n) {
return [] if n.ilog10.is_even
 
Line 3,691:
var fangs = is_vampire(n)
say "#{n}: #{fangs ? fangs.join(', ') : 'is not a vampire number'}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,729:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
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)")
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,817:
=={{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,840:
}
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,866:
puts "$n is not a vampire number"
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,902:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">import math
fn max(a u64, b u64) u64 {
if a > b {
Line 3,995:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 1: 1260 = 21 × 60
Line 4,043:
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var ndigits = Fn.new { |x|
Line 4,115:
Fmt.print("$d is not vampiric", x)
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,162:
 
=={{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 4,174:
});
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 4,183:
 
(0).walker(*).tweak(fangs).filter(26)
.pump(Console.println,vampiric);</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits