Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,9 +1,9 @@
|
|||
import std.stdio, std.range, std.algorithm, std.typecons, std.conv;
|
||||
|
||||
auto fangs(in long n) {
|
||||
auto fangs(in long n) pure nothrow @safe {
|
||||
auto pairs = iota(2, cast(int)(n ^^ 0.5)) // n.isqrt
|
||||
.filter!(x => !(n % x)).map!(x => [x, n / x]);
|
||||
enum dLen = (long x) => x.text.length;
|
||||
enum dLen = (in long x) => x.text.length;
|
||||
immutable half = dLen(n) / 2;
|
||||
enum halvesQ = (long[] p) => p.all!(u => dLen(u) == half);
|
||||
enum digits = (long[] p) => dtext(p[0], p[1]).dup.sort();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.math, std.algorithm, std.array, std.traits;
|
||||
|
||||
T[N] pows(T, size_t N)() {
|
||||
T[N] pows(T, size_t N)() pure nothrow @safe @nogc {
|
||||
typeof(return) result;
|
||||
result[0] = 1;
|
||||
foreach (immutable i, ref r; result[1 .. $])
|
||||
|
|
@ -11,7 +11,7 @@ T[N] pows(T, size_t N)() {
|
|||
__gshared immutable tenPowsU = pows!(uint, 10);
|
||||
__gshared immutable tenPowsUL = pows!(ulong, 20);
|
||||
|
||||
size_t nDigits(T)(in T x) pure nothrow {
|
||||
size_t nDigits(T)(in T x) pure nothrow @safe @nogc {
|
||||
Unqual!T y = x;
|
||||
size_t n = 0;
|
||||
while (y) {
|
||||
|
|
@ -21,7 +21,7 @@ size_t nDigits(T)(in T x) pure nothrow {
|
|||
return n;
|
||||
}
|
||||
|
||||
T dTally(T)(in T x) pure nothrow {
|
||||
T dTally(T)(in T x) pure nothrow @safe @nogc {
|
||||
Unqual!T y = x;
|
||||
T t = 0;
|
||||
while (y) {
|
||||
|
|
@ -32,7 +32,7 @@ T dTally(T)(in T x) pure nothrow {
|
|||
}
|
||||
|
||||
T[] fangs(T)(in T x, T[] f)
|
||||
pure nothrow if (is(T == uint) || is(T == ulong)) {
|
||||
pure nothrow @safe @nogc if (is(T == uint) || is(T == ulong)) {
|
||||
alias tenPows = Select!(is(T == ulong), tenPowsUL, tenPowsU);
|
||||
|
||||
immutable nd0 = nDigits(x);
|
||||
|
|
@ -78,9 +78,9 @@ void main() {
|
|||
}
|
||||
writeln;
|
||||
|
||||
__gshared static immutable ulong[3] bigs = [16_758_243_290_880UL,
|
||||
24_959_017_348_650UL,
|
||||
14_593_825_548_650UL];
|
||||
static immutable ulong[3] bigs = [16_758_243_290_880UL,
|
||||
24_959_017_348_650UL,
|
||||
14_593_825_548_650UL];
|
||||
ulong[fu.length] ful;
|
||||
foreach (immutable bi; bigs) {
|
||||
const fs = fangs(bi, ful);
|
||||
|
|
|
|||
70
Task/Vampire-number/Java/vampire-number-2.java
Normal file
70
Task/Vampire-number/Java/vampire-number-2.java
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
public class VampireNumber {
|
||||
|
||||
public static void main(String args[]) {
|
||||
|
||||
// scan only the ranges that have an even number of digits
|
||||
// for instance: 10 .. 99, 1000 .. 9999 etc
|
||||
long countVamps = 0, start = 10, tens = 10;
|
||||
outer:
|
||||
for (int numDigits = 2; numDigits <= 18; numDigits += 2) {
|
||||
long end = start * 10;
|
||||
for (long i = start; i < end; i++) {
|
||||
if (countFangs(i, tens) > 0) {
|
||||
if (++countVamps >= 26)
|
||||
break outer;
|
||||
}
|
||||
}
|
||||
start *= 100;
|
||||
tens *= 10;
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
long[] bigs = {16758243290880L, 24959017348650L,
|
||||
14593825548650L};
|
||||
|
||||
for (long b : bigs)
|
||||
countFangs(b, 10000000L);
|
||||
}
|
||||
|
||||
private static int countFangs(long n, long tens) {
|
||||
int countFangs = 0;
|
||||
|
||||
// limit the search space for factors (as in C example)
|
||||
long lo = Math.max(tens / 10, (n + tens - 2) / (tens - 1));
|
||||
long hi = Math.min(n / lo, (long) Math.sqrt(n));
|
||||
|
||||
long nTally = tallyDigits(n);
|
||||
|
||||
for (long a = lo; a <= hi; a++) {
|
||||
long b = n / a;
|
||||
|
||||
if (a * b != n)
|
||||
continue;
|
||||
|
||||
// check for mod 9 congruence
|
||||
if (n % 9 != (a + b) % 9)
|
||||
continue;
|
||||
|
||||
if (a % 10 == 0 && b % 10 == 0)
|
||||
continue;
|
||||
|
||||
if (nTally == tallyDigits(a) + tallyDigits(b)) {
|
||||
if (countFangs == 0)
|
||||
System.out.printf("\n%d : ", n);
|
||||
System.out.printf("[%d, %d]", a, b);
|
||||
countFangs++;
|
||||
}
|
||||
}
|
||||
return countFangs;
|
||||
}
|
||||
|
||||
// sum to a unique number to represent set of digits (as in C example)
|
||||
private static long tallyDigits(long n) {
|
||||
long total = 0;
|
||||
while (n > 0) {
|
||||
total += 1L << ((n % 10) * 6);
|
||||
n /= 10;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
27
Task/Vampire-number/Mathematica/vampire-number-1.math
Normal file
27
Task/Vampire-number/Mathematica/vampire-number-1.math
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
ClearAll[VampireQ]
|
||||
VampireQ[num_Integer] := Module[{poss, divs},
|
||||
divs = Select[Divisors[num], # <= Sqrt[num] &];
|
||||
poss = {#, num/#} & /@ divs;
|
||||
If[Length[poss] > 0,
|
||||
poss = Select[poss, Mod[#, 10] =!= {0, 0} &];
|
||||
If[Length[poss] > 0,
|
||||
poss = Select[poss, Length[IntegerDigits[First[#]]] == Length[IntegerDigits[Last[#]]] &];
|
||||
If[Length[poss] > 0,
|
||||
poss = Select[poss, Sort[IntegerDigits[num]] == Sort[Join @@ (IntegerDigits /@ #)] &];
|
||||
If[Length[poss] > 0
|
||||
,
|
||||
Sow[{num, poss}];
|
||||
True
|
||||
,
|
||||
False
|
||||
]
|
||||
,
|
||||
False
|
||||
]
|
||||
,
|
||||
False
|
||||
]
|
||||
,
|
||||
False
|
||||
]
|
||||
]
|
||||
2
Task/Vampire-number/Mathematica/vampire-number-2.math
Normal file
2
Task/Vampire-number/Mathematica/vampire-number-2.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Reap[Scan[VampireQ,Range[126027]]][[2,1]]//Grid
|
||||
Reap[VampireQ[#]] & /@ {16758243290880, 24959017348650, 14593825548650} // Grid
|
||||
|
|
@ -1,45 +1,46 @@
|
|||
/*REXX pgm displays X vampire numbers, or verifies if a # is vampiric.*/
|
||||
/*REXX pgm displays N vampire numbers, or verifies if a # is vampiric.*/
|
||||
numeric digits 20 /*be able to handle large numbers*/
|
||||
parse arg N .; if N=='' then N=25 /*No arg? 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 /*lowest#,dig*/
|
||||
#=0 /*number of vampire numbers found*/
|
||||
if N>0 then do j=1260 until # >= N /*search until N vampire #s found*/
|
||||
if length(j)//2 then do; j=j*10-1; iterate; end /*adjust J*/
|
||||
if n<11453481 then if right(j,1)==1 then iterate /*tenable?*/
|
||||
f=vampire(j) /*get possible fangs for J. */
|
||||
if f=='' then iterate /*Is 2nd fang null? Yes, not vamp*/
|
||||
#=#+1 /*bump the vampire count, Vlad. */
|
||||
say 'vampire number' right(#,length(N)) "is: " j', fangs=' f
|
||||
end /*j*/
|
||||
else do
|
||||
f=vampire(-N) /*get possible fangs for abs(N).*/
|
||||
if f=='' then say -N " isn't a vampire number."
|
||||
else say -N " is a vampire number, fangs=" f
|
||||
end /*j*/
|
||||
if N>0 then do j=1260 until # >= N /*search until N vampire #s found*/
|
||||
if length(j)//2 then do; j=j*10-1; iterate; end /*adjust J*/
|
||||
_=right(j,1) /*obtain the right-most J digit.*/
|
||||
if j<!._ then iterate /*is # tenable based on last dig?*/
|
||||
f=vampire(j) /*get possible fangs for J. */
|
||||
if f=='' then iterate /*Are fangs null? Yes, ¬vampire.*/
|
||||
#=#+1 /*bump the vampire count, Vlad. */
|
||||
say 'vampire number' right(#,length(N)) "is: " j', fangs=' f
|
||||
end /*j*/ /* [↑] process range of numbers.*/
|
||||
else do; N=abs(N) /* [↓] process a particular num.*/
|
||||
f=vampire(N) /*get possible fangs for abs(N).*/
|
||||
if f=='' then say N " isn't a vampire number."
|
||||
else say N " is a vampire number, fangs=" f
|
||||
end
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────VAMPIRE subroutine──────────────────*/
|
||||
vampire: procedure; parse arg ?; L=length(?); W=L%2
|
||||
if L//2 then return '' /*Odd length? Then not vampire.*/
|
||||
$.= /*used to build BOT and TOP value*/
|
||||
fangs=; do k=1 for L; _=substr(?,k,1); $._=$._ || _; end /*k*/
|
||||
vampire: procedure; parse arg ?,,$. !!; L=length(?); w=L%2
|
||||
if L//2 then return !! /*Odd length? Then not vampire.*/
|
||||
do k=1 for L; _=substr(?,k,1); $._=$._ || _; end /*k*/
|
||||
bot=; do m=0 for 10; bot=bot || $.m; end /*m*/
|
||||
|
||||
top=left(reverse(bot),w); bot=left(bot,w) /*determine limits of search*/
|
||||
|
||||
do d=max(bot, 10**(w-1)) to min(top, 10**w-1)
|
||||
if ?//d\==0 then iterate
|
||||
if verify(d,?)\==0 then iterate
|
||||
if ?//d\==0 then iterate
|
||||
if verify(d,?)\==0 then iterate
|
||||
q=?%d; if d>q then iterate
|
||||
if q*d//9\==(q+d)//9 then iterate /*modulo 9 congruence.*/
|
||||
if length(q)\==w then iterate
|
||||
if verify(q,?)\==0 then iterate
|
||||
if right(q,1)==0 then if right(d,1)==0 then iterate
|
||||
dq=d || q; t=?
|
||||
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*/
|
||||
fangs=fangs d','q
|
||||
end /*d*/
|
||||
return fangs
|
||||
if ?//2 then inc=2 /*if ? is odd, set INC to 2.*/
|
||||
else inc=1 /*··otherwise, set INC to 1.*/
|
||||
start=max(bot,10**(w-1)); if inc=2 then if start//2==0 then start=start+1
|
||||
/* [↑] odd START if odd INC*/
|
||||
do d=start to min(top, 10**w-1) by inc
|
||||
if ?//d\==0 then iterate
|
||||
if verify(d,?)\==0 then iterate
|
||||
q=?%d; if d>q then iterate
|
||||
if q*d//9\==(q+d)//9 then iterate /*modulo 9 congruence.*/
|
||||
if verify(q,?)\==0 then iterate
|
||||
if right(q,1)==0 then if right(d,1)==0 then iterate
|
||||
if length(q)\==w then iterate
|
||||
dq=d || q; t=?
|
||||
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*/
|
||||
!!=!! '['d"∙"q']'
|
||||
end /*d*/
|
||||
return !!
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
def factor_pairs n
|
||||
first = 10 ** ((n.to_s.size / 2) - 1)
|
||||
first = n / (10 ** (n.to_s.size / 2) - 1)
|
||||
(first .. n ** 0.5).map { |i| [i, n / i] if n % i == 0 }.compact
|
||||
end
|
||||
|
||||
|
|
@ -26,6 +26,6 @@ end
|
|||
if (vf = vampire_factors n).empty?
|
||||
puts "#{n} is not a vampire number!"
|
||||
else
|
||||
puts "#{n}:\t#{vf}" unless (vf = vampire_factors n).empty?
|
||||
puts "#{n}:\t#{vf}"
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue