First perfect square in base n with n unique digits: Difference between revisions

→‎{{header|C#|Csharp}}: fixed problem with base28
m (→‎{{header|C#|Csharp}}: oops, problem with base 28, will fix later)
(→‎{{header|C#|Csharp}}: fixed problem with base28)
Line 174:
// parse a string into a BigInteger, using current base
static BigInteger to10(string s) {
BigInteger res = 0; foreach (char i in s) res = res * Base + chars.IndexOf(i);
res = res * Base + chars.IndexOf(i);
return res;
}
Line 181 ⟶ 180:
// returns the minimum value string, optionally inserting extra digit
static string fixup(int n) {
string res = chars.Substring(0, Base); if (n > 0) res = res.Insert(n, n.ToString());
if (n > 0) res = res.Insert(n, n.ToString());
return "10" + res.Substring(2);
}
Line 188 ⟶ 186:
// checks the square against the threshold, advances various limits when needed
static void check(BigInteger sq) {
if (sq > threshold && blim > 0) {
o.Remove((byte)chars.IndexOf(ms[blim])); blim -= 1; ic -= 1;
threshold = limits[bmo - blim - 1]; bllim = to10(ms.Substring(0, blim + 1));
}
}
 
// performs all the caclulations for the current base
static void doOne() {
{
limits = new List<BigInteger>();
bmo = (byte)(Base - 1); byte dr = 0; if ((Base & 1) == 1) dr = (byte)(Base >> 1);
Line 203 ⟶ 200:
byte[] sdr = new byte[bmo]; byte rc = 0; for (i = 0; i < bmo; i++) {
sdr[i] = (byte)((i * i) % bmo); rc += sdr[i] == dr ? (byte)1 : (byte)0;
sdr[i] += sdr[i] == 0 ? bmo : (byte)0; } i = 0; if (dr > 0) { id = Base;
} i = 0; if (dr > 0) {
resid = res * Base + chars.IndexOf(i);
for (i = 1; i <= dr; i++) if (sdr[i] >= dr) if (id > sdr[i]) id = sdr[i]; id -= dr;
i = 0; } ms = fixup(id);
} ms = fixup(id);
BigInteger sq = to10(ms); BigInteger rt = new BigInteger(Math.Sqrt((double)sq) + 1);
sq = rt * rt; if (Base > 9) { for (int j = 1; j < Base; j++)
for (int j = 1; j < Base; j++)
limits.Add(to10(ms.Substring(0, j) + new string(chars[bmo], Base - j + (rc > 0 ? 0 : 1))));
limits.Reverse(); while (sq < limits[0]) { rt++; sq = rt * rt; }
Line 213 ⟶ 214:
BigInteger dn = (rt << 1) + 1; BigInteger d = 1; if (Base > 3 && rc > 0) {
while (sq % bmo != dr) { rt += 1; sq += dn; dn += 2; } // alligns sq to dr
inc = bmo / rc; if (inc > 1) { dn += rt * (inc - 2) - 1; d = inc * inc; }
if (inc > 1) { dn += rt * (inc - 2) - 1; d = inc * inc; }
dn += dn + d;
} d <<= 1; if (Base > 9) {
d <<= 1; if (Base > 9) {
blim = 0; while (sq < limits[bmo - blim - 1]) blim++; ic = (byte)(blim + 1);
threshold = limits[bmo - blim - 1];
if (blim > 0) for (byte j = 0; j < bmo -= blim; j++) o.Add((byte)chars.IndexOf(ms[j]));
if (blim > 0) bllim = to10(ms.Substring(0, blim + 1)); else bllim = 0;
if (Base > 5 && rc > 0)
Line 231 ⟶ 234:
} rt += i * inc;
Console.WriteLine("{0,3} {1,2} {2,2} {3,20} -> {4,-40} {5,10} {6,9:0.0000}s {7,9:0.0000}s",
Base, inc, (id > 0 ? chars.Substring(id, 1) : " "), toStr(rt), toStr(sq), i,
(DateTime.Now - st).TotalSeconds, (DateTime.Now - st0).TotalSeconds);
}
Line 238 ⟶ 241:
Console.WriteLine("base inc id root square" +
" test count time total");
for (Base = 2; Base <= 2728; Base++) doOne();
Console.WriteLine("Elasped time was {0,8:0.00} minutes", (DateTime.Now - st0).TotalMinutes);
}
Line 250 ⟶ 253:
6 5 523 -> 452013 20 0.0000s 0.0060s
7 6 1431 -> 2450361 34 0.0000s 0.0060s
8 7 3344 -> 13675420 41 0.0010s0000s 0.0070s0060s
9 4 11642 -> 136802574 289 0.0000s0010s 0.0070s
10 3 32043 -> 1026753849 17 0.0080s0050s 0.0150s0120s
11 10 111453 -> 1240A536789 1498 0.0010s 0.0160s0130s
12 11 3966B9 -> 124A7B538609 6883 0.0030s0040s 0.0189s0170s
13 1 3 3828943 -> 10254773CA86B9 8242 0.0311s0439s 0.0500s0609s
14 13 3A9DB7C -> 10269B8C57D3A4 1330 0.0020s0010s 0.0520s0619s
15 14 1012B857 -> 102597BACE836D4 4216 0.0020s 0.0540s0638s
16 15 404A9D9B -> 1025648CFEA37BD9 18457 0.0100s 0.0640s0738s
17 1 1 423F82GA9 -> 101246A89CGFB357ED 195112 0.2802s2783s 0.3443s3521s
18 17 44B482CAD -> 10236B5F8EG4AD9CH7 30440 0.0190s0199s 0.3632s3720s
19 6 1011B55E9A -> 10234DHBG7CI8F6A9E5 93021 0.0608s0589s 0.4241s4309s
20 19 49DGIH5D3G -> 1024E7CDI3HB695FJA8G 11310604 76.1399s9833s 7.5640s4142s
21 1 6 4C9HE5FE27F -> 1023457DG9HI8J6B6KCEAF 601843 1.0841s0871s 8.6480s5013s
22 21 4F94788GJ0F -> 102369FBGDEJ48CHI7LKA5 27804949 18.2373s3290s 26.8863s8302s
23 22 1011D3EL56MC -> 10234ACEDKG9HM8FBJIL756 17710217 1611.2366s4105s 4338.1229s2407s
24 23 4LJ0HDGF0HD3 -> 102345B87HFECKJNIGMDLA69 4266555 2.5262s4763s 4540.6491s7171s
25 12 1011E145FHGHM -> 102345DOECKJ6GFB8LIAM7NH9 78092124 7552.5179s6831s 121 93.1671s4012s
26 5 52K8N53BDM99K -> 1023458LO6IEMKG79FPCHNJDBA 402922568 286287.1066s9058s 407381.2737s3080s
27 26 1011F11E37OBJJ -> 1023458ELOMDHBIJFGKP7CQ9N6A 457555293 466326.8900s1714s 874707.1636s4794s
28 9 58A3CKP3N4CQD7 -> 1023456CGJBIRQEDHP98KMOAN7FL 749592976 508.4498s 1215.9292s
Elasped time was 20.27 minutes
</pre>