2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -2,12 +2,22 @@ A [[wp:Vampire_number|vampire number]] is a natural number with an even number o
|
|||
* 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
|
||||
An example of a Vampire number and its fangs: <code>1260 : (21, 60)</code>
|
||||
;<nowiki>Task description:</nowiki>
|
||||
|
||||
<br>
|
||||
An example of a Vampire number and its fangs: <code> 1260 : (21, 60) </code>
|
||||
|
||||
|
||||
;Task:
|
||||
# Print the first 25 Vampire numbers and their fangs.
|
||||
# Check if the following numbers are Vampire numbers and, if so, print them and their fangs: <code>16758243290880, 24959017348650, 14593825548650</code>
|
||||
Note that a Vampire number can have more than 1 pair of fangs.
|
||||
;<nowiki>See also:</nowiki>
|
||||
# Check if the following numbers are Vampire numbers and, if so, print them and their fangs:
|
||||
<big><code> 16758243290880, 24959017348650, 14593825548650 </code></big>
|
||||
|
||||
<br>
|
||||
Note that a Vampire 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/ Vampire search algorithm]
|
||||
* [[oeis:A014575|Vampire numbers on OEIS]]
|
||||
<br><br>
|
||||
|
|
|
|||
42
Task/Vampire-number/Elixir/vampire-number.elixir
Normal file
42
Task/Vampire-number/Elixir/vampire-number.elixir
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
defmodule Vampire do
|
||||
def factor_pairs(n) do
|
||||
first = trunc(n / :math.pow(10, div(char_len(n), 2)))
|
||||
last = :math.sqrt(n) |> round
|
||||
for i <- first .. last, rem(n, i) == 0, do: {i, div(n, i)}
|
||||
end
|
||||
|
||||
def vampire_factors(n) do
|
||||
if rem(char_len(n), 2) == 1 do
|
||||
[]
|
||||
else
|
||||
half = div(length(to_char_list(n)), 2)
|
||||
sorted = Enum.sort(String.codepoints("#{n}"))
|
||||
Enum.filter(factor_pairs(n), fn {a, b} ->
|
||||
char_len(a) == half && char_len(b) == half &&
|
||||
Enum.count([a, b], fn x -> rem(x, 10) == 0 end) != 2 &&
|
||||
Enum.sort(String.codepoints("#{a}#{b}")) == sorted
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
defp char_len(n), do: length(to_char_list(n))
|
||||
|
||||
def task do
|
||||
Enum.reduce_while(Stream.iterate(1, &(&1+1)), 1, fn n, acc ->
|
||||
case vampire_factors(n) do
|
||||
[] -> {:cont, acc}
|
||||
vf -> IO.puts "#{n}:\t#{inspect vf}"
|
||||
if acc < 25, do: {:cont, acc+1}, else: {:halt, acc+1}
|
||||
end
|
||||
end)
|
||||
IO.puts ""
|
||||
Enum.each([16758243290880, 24959017348650, 14593825548650], fn n ->
|
||||
case vampire_factors(n) do
|
||||
[] -> IO.puts "#{n} is not a vampire number!"
|
||||
vf -> IO.puts "#{n}:\t#{inspect vf}"
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
Vampire.task
|
||||
|
|
@ -1,8 +1,19 @@
|
|||
my @vampires := gather for 1 .. * -> $start, $end {
|
||||
map {
|
||||
my @fangs = is_vampire($_);
|
||||
take "$_: { @fangs.join(', ') }" if @fangs.elems
|
||||
}, 10 ** $start .. 10 ** $end
|
||||
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";
|
||||
|
|
@ -21,18 +32,6 @@ for 16758243290880, 24959017348650, 14593825548650 {
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
sub vfactors (Int $n) {
|
||||
map { $_ if $n %% $_ }, 10**$n.sqrt.log(10).floor .. $n.sqrt.ceiling;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue