|
Still running...85 minutes in...
currently at:
Loop 76: Length 881752750
|
|
|
|
|
85 MINUTES?! You'll be running this for about a week to get to 100.
It can be done a lot quicker than that. The 76th number took 12 seconds on my machine and it's a "nothing special" machine.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
I thought I would run out ot memory and did it writing to a file...not the smartest idea...now I just can't bring myself to stop the run.
|
|
|
|
|
1) Strings and string methods are not going to do it. They're too slow and take up too much memory.
2) The only digits you see in any of these numbers are 1, 2, and 3. It seems like a waste to use an entire byte to store each digit.
3) If you graph the math on the progression of the length of these numbers, you'll see that on a LOGARITHMIC SCALE, the graph is about a 40 degree line. What would that look like on a normal X/Y scale?
4) You cannot do this "in memory", without going to the extremes of cleverness, and even then, you'd still need a gargantuan amount of RAM.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
Good hints...gonna have another crack at this back home.
|
|
|
|
|
The length of row n won't exceed twice the length of row n-1 , yes?
The result is computable, therefore a Turing Machine can compute it, and, because Turing Machines have virtually unlimited storage, simply use one.
|
|
|
|
|
You build the machine and I'll go make the infinite paper tape.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
The spec isn't clear! Send it back!
As this is, in essence, a compression algorithm, at line 8->9 (according to the OEIS) I would do:
1113213211
11 132132 11 <== three subsequences
21 2132 21 <== three outputs, eight digits
Which is shorter than their naive result of:
1113213211
111 3 2 1 3 2 11 <== seven subsequences
31 13 12 11 13 12 21 <== seven outputs, fourteen digits
A 40% saving.
The complexity of the algorithm increases due to seeking how to split the input into the fewest subsequences of some repetition length (1 in the naive implementation).
|
|
|
|
|
When in the is the spec Everclear[^] ?
Project Euler specs aren't clear either. We always have to do the best we can with what we've got.
1113213211
11 132132 11 <== 13?
21 132132 21 <== three outputs, eight digits
What happened to the 13? The output looks like it should be 10 digits, not 8.
1113213211
111 32132 11
31 32132 21 <== if I understand what you're trying to do
There seems to a problem with representation. How do you tell the difference between single values and a run length value?
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
Dave Kreskowiak wrote: What happened to the 13?
There are 2 132s , hence 2132 .
Dave Kreskowiak wrote: How do you tell the difference between single values and a run length value?
Doesn't matter, but internally (if I write it) it would be in the data structure. It just wouldn't be apparent in the output unless you want it.
(1,1)
(2,1)
...
(2,1),(2,132),(2,1)
...
The question is about only the number of digits.
|
|
|
|
|
Ah, OK. I missed that.
Hmmm. In my implementation, I wrote up a reader/writer that takes care of the <classified work="" for="" now=""> "on the fly". This would make an interesting, and challenging, implementation to write. I'll have to look into trying this next weekend.
My current implementation writes all the data but there is an option to convert the data to a human-readable format. Not that you'd want to see thousands of pages of 1's, 2's, and 3's, but it did come in handy for analysis when experimenting with implementations.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
340472211484 approx (via log extrapolation)
|
|
|
|
|
Nope, not even close.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
After a bit more fiddling:
Test length 48 th : 526646 526,646
Test length 49 th : 686646 686,646
Test length 50 th : 894810 894,810
51st length : 1,166,642
52nd length : 1,521,070
53rd length : 1,983,164
54th length : 2,585,639
55th length : 3,371,142
56th length : 4,395,278
57th length : 5,730,540
58th length : 7,471,449
59th length : 9,741,236
60th length : 12,700,573
61st length : 16,558,941
62nd length : 21,589,461
63rd length : 28,148,228
64th length : 36,699,513
65th length : 47,848,635
66th length : 62,384,802
67th length : 81,336,981
68th length : 106,046,733
69th length : 138,263,181
70th length : 180,266,818
71st length : 235,030,941
72nd length : 306,432,122
73rd length : 399,524,610
74th length : 520,898,113
75th length : 679,144,257
76th length : 885,464,758
77th length : 1,154,464,356
78th length : 1,505,184,637
79th length : 1,962,451,918
80th length : 2,558,634,627
81st length : 3,335,934,550
82nd length : 4,349,374,155
83rd length : 5,670,691,453
84th length : 7,393,418,089
85th length : 9,639,500,137
86th length : 12,567,930,256
87th length : 16,386,002,249
88th length : 21,363,984,700
89th length : 27,854,252,387
90th length : 36,316,229,718
91st length : 47,348,911,849
92nd length : 61,733,265,560
93rd length : 80,487,511,283
94th length : 104,939,199,534
95th length : 136,819,183,789
96th length : 178,384,141,824
97th length : 232,576,318,416
98th length : 303,231,797,036
99th length : 395,352,043,407
100th length : 515,457,942,582
Regards , R
|
|
|
|
|
Wrong again!
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
Dave Kreskowiak wrote:Wrong again!
100 : 511247092564
Exactly this time !
Regards, R
|
|
|
|
|
Great!
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
What base?
The length is 10 -- in some base I haven't determined yet.
modified 2-Dec-17 1:10am.
|
|
|
|
|
So I stored booleans in a file:
string Morris(int S, int N)
{
string projectPath = System.IO.Path.GetFullPath(@"..\..\..\");
using (BinaryWriter writer = new BinaryWriter(File.Open(projectPath + "input.txt", FileMode.Create)))
{
writer.Write(S > 2);
writer.Write(S == 2);
}
for (int i = 1; i < N; i++)
{
Debug.WriteLine(i+1);
using (BinaryReader reader = new BinaryReader(File.Open(projectPath + "input.txt", FileMode.Open)))
{
int count = 1;
bool currMSB = reader.ReadBoolean();
bool currLSB = reader.ReadBoolean();
bool nextMSB, nextLSB;
using (BinaryWriter writer = new BinaryWriter(File.Open(projectPath + "output.txt", FileMode.Create)))
{
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
nextMSB = reader.ReadBoolean();
nextLSB = reader.ReadBoolean();
if ((currMSB == nextMSB) && (currLSB == nextLSB))
{
count++;
}
else
{
writer.Write(count > 2);
writer.Write(count == 2);
writer.Write(currMSB);
writer.Write(currLSB);
currMSB = nextMSB;
currLSB = nextLSB;
count = 1;
}
}
writer.Write(count > 2);
writer.Write(count == 2);
writer.Write(currMSB);
writer.Write(currLSB);
}
}
File.Delete(projectPath + "input.txt");
System.IO.File.Copy(projectPath + "output.txt", projectPath + "input.txt");
System.IO.File.WriteAllText(projectPath + "output.txt", string.Empty);
}
StringBuilder output = new StringBuilder();
using (BinaryReader reader = new BinaryReader(File.Open(projectPath + "input.txt", FileMode.Open)))
{
bool nextMSB, nextLSB;
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
nextMSB = reader.ReadBoolean();
nextLSB = reader.ReadBoolean();
if (nextMSB)
output.Append("3");
else
{
if (nextLSB)
output.Append("2");
else
output.Append("1");
}
}
}
return output.ToString();
}
Seems to work quite well, but what did you do?
|
|
|
|
|
Interesting but I question if this is actually writing one byte per value? Don't have time to test right now.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
I suspect that it is using a byte for each boolean value. As per the usual answers:
Why is a boolean 4 bytes in .NET? - Stack Overflow[^]
I could store them in a BitVector32 or a BitArray and write that to the file, but I don't have the time to implement it now.
|
|
|
|
|
I tried doing this in a BitArray, but found it to be limited in flexibility and performance. This was about 10 years that I originally worked on this problem.
I was doing some cleaning around the drive to get rid of old stuff and ran into the project. Then, of course, I just had to run it again and maybe update the code a little bit.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
They definitely store the booleans as bytes. I ran this:
string MorrisBitVector32(int S, int N)
{
int[] _masks = new int[32];
{
_masks[0] = BitVector32.CreateMask();
}
for (int i = 1; i < 32; i++)
{
_masks[i] = BitVector32.CreateMask(_masks[i - 1]);
}
string projectPath = System.IO.Path.GetFullPath(@"..\..\..\");
using (BinaryWriter writer = new BinaryWriter(File.Open(projectPath + "input.txt", FileMode.Create)))
{
BitVector32 v = new BitVector32();
v[_masks[0]] = S >= 2;
v[_masks[1]] = S != 2;
writer.Write(v.Data);
}
for (int i = 1; i < N; i++)
{
Debug.WriteLine(i + 1);
using (BinaryReader reader = new BinaryReader(File.Open(projectPath + "input.txt", FileMode.Open)))
{
bool currMSB, currLSB, firstRun;
firstRun = true;
currMSB = false;
currLSB = false;
int count = 0;
int k = 0;
BitVector32 outputBits = new BitVector32();
using (BinaryWriter writer = new BinaryWriter(File.Open(projectPath + "output.txt", FileMode.Create)))
{
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
BitVector32 inputBits = new BitVector32(reader.ReadInt32());
if (firstRun)
{
count = 1;
currMSB = inputBits[_masks[0]];
currLSB = inputBits[_masks[1]];
}
bool nextMSB, nextLSB;
for (int j = (firstRun ? 2 : 0); j < 32; j += 2)
{
nextMSB = inputBits[_masks[j]];
nextLSB = inputBits[_masks[j + 1]];
if (!(nextLSB || nextMSB))
{
break;
}
if ((currMSB == nextMSB) && (currLSB == nextLSB))
{
count++;
}
else
{
if (k == 32)
{
writer.Write(outputBits.Data);
outputBits = new BitVector32();
k = 0;
outputBits[_masks[k]] = count >= 2;
k++;
outputBits[_masks[k]] = count != 2;
k++;
outputBits[_masks[k]] = currMSB;
k++;
outputBits[_masks[k]] = currLSB;
k++;
currMSB = nextMSB;
currLSB = nextLSB;
count = 1;
}
else
{
outputBits[_masks[k]] = count >= 2;
k++;
outputBits[_masks[k]] = count != 2;
k++;
outputBits[_masks[k]] = currMSB;
k++;
outputBits[_masks[k]] = currLSB;
k++;
currMSB = nextMSB;
currLSB = nextLSB;
count = 1;
}
}
if (firstRun)
firstRun = false;
}
}
if (k == 32)
{
writer.Write(outputBits.Data);
outputBits = new BitVector32();
k = 0;
outputBits[_masks[k]] = count >= 2;
k++;
outputBits[_masks[k]] = count != 2;
k++;
outputBits[_masks[k]] = currMSB;
k++;
outputBits[_masks[k]] = currLSB;
k++;
writer.Write(outputBits.Data);
}
else
{
outputBits[_masks[k]] = count >= 2;
k++;
outputBits[_masks[k]] = count != 2;
k++;
outputBits[_masks[k]] = currMSB;
k++;
outputBits[_masks[k]] = currLSB;
k++;
writer.Write(outputBits.Data);
}
}
}
File.Delete(projectPath + "input.txt");
System.IO.File.Copy(projectPath + "output.txt", projectPath + "input.txt");
System.IO.File.WriteAllText(projectPath + "output.txt", string.Empty);
}
StringBuilder output = new StringBuilder();
using (BinaryReader reader = new BinaryReader(File.Open(projectPath + "input.txt", FileMode.Open)))
{
bool nextMSB, nextLSB;
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
BitVector32 InputData = new BitVector32(reader.ReadInt32());
for (int i = 0; i < 32; i+=2)
{
nextMSB = InputData[_masks[i]];
nextLSB= InputData[_masks[i+1]];
if (!(nextLSB || nextMSB))
break;
if (nextMSB && nextLSB)
output.Append("3");
else
{
if (nextMSB)
output.Append("2");
else
output.Append("1");
}
}
}
}
return output.ToString();
}
Files are now very small, and it is reasonably fast.
|
|
|
|
|
My instant impression of it is that there has to be a better way than brute force: there's something very Fibonacci-sequence-like about the output... in my head, I can almost predict the pattern from one iteration to the next, without trying to describe anything... if only I had better coffee... if only Dijkstra were still alive... damn it: now you've got me interested.
|
|
|
|
|
I know there has to be a better way to do it because I did find a list that gave the lengths for the first 3000 numbers in the sequence. Let's just say there are more digits in the 3000th number than there are atoms in the observable universe. I'll post the answer and the length of #3000 Monday morning.
It does make for any interesting problem!
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|