Coding Challenge - Morris Sequence
-
It's also known as the Conway Sequence, Look and Say Sequence, and probably some others. It's rather simple. Start with a 1 and then describe what you see for the next iteration. So, starting at 1, the next number is one 1 (11), the next is two 1 (21), then one 2 one 1 (1211), and so on:
1
11
21
1211
111221
312211The question to answer is what's the length in digits of the 100th number in the chain, starting with "1" as the first? The first six numbers have been given above. You could write it out by hand, but I wouldn't recommend it, and as developers, that's not what we do. The seemingly simple challenge is to write the code to come up with the answer. The only hint you get is the 50th number is 894,810 digits long. Oh, and don't bother Googling for code. Those examples will only get you so far and definitely won't get you to the answer.
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave Kreskowiak -
It's also known as the Conway Sequence, Look and Say Sequence, and probably some others. It's rather simple. Start with a 1 and then describe what you see for the next iteration. So, starting at 1, the next number is one 1 (11), the next is two 1 (21), then one 2 one 1 (1211), and so on:
1
11
21
1211
111221
312211The question to answer is what's the length in digits of the 100th number in the chain, starting with "1" as the first? The first six numbers have been given above. You could write it out by hand, but I wouldn't recommend it, and as developers, that's not what we do. The seemingly simple challenge is to write the code to come up with the answer. The only hint you get is the 50th number is 894,810 digits long. Oh, and don't bother Googling for code. Those examples will only get you so far and definitely won't get you to the answer.
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave KreskowiakThat sequence apparently has other uses: :-D Saturday Morning Breakfast Cereal - Mathematical Methods[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
That sequence apparently has other uses: :-D Saturday Morning Breakfast Cereal - Mathematical Methods[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
:laugh: :laugh: :laugh:
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave Kreskowiak -
It's also known as the Conway Sequence, Look and Say Sequence, and probably some others. It's rather simple. Start with a 1 and then describe what you see for the next iteration. So, starting at 1, the next number is one 1 (11), the next is two 1 (21), then one 2 one 1 (1211), and so on:
1
11
21
1211
111221
312211The question to answer is what's the length in digits of the 100th number in the chain, starting with "1" as the first? The first six numbers have been given above. You could write it out by hand, but I wouldn't recommend it, and as developers, that's not what we do. The seemingly simple challenge is to write the code to come up with the answer. The only hint you get is the 50th number is 894,810 digits long. Oh, and don't bother Googling for code. Those examples will only get you so far and definitely won't get you to the answer.
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave KreskowiakNow why do I want to hit you with a stick?
-
Now why do I want to hit you with a stick?
Because you're curious and have no idea why you're driven to solve the problem? :-D
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave Kreskowiak -
It's also known as the Conway Sequence, Look and Say Sequence, and probably some others. It's rather simple. Start with a 1 and then describe what you see for the next iteration. So, starting at 1, the next number is one 1 (11), the next is two 1 (21), then one 2 one 1 (1211), and so on:
1
11
21
1211
111221
312211The question to answer is what's the length in digits of the 100th number in the chain, starting with "1" as the first? The first six numbers have been given above. You could write it out by hand, but I wouldn't recommend it, and as developers, that's not what we do. The seemingly simple challenge is to write the code to come up with the answer. The only hint you get is the 50th number is 894,810 digits long. Oh, and don't bother Googling for code. Those examples will only get you so far and definitely won't get you to the answer.
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave KreskowiakI think I got a working code:
string LookAndSay(int S, int n) { string result = S.ToString(); for (int i = 0; i < (n - 1); i++) { StringBuilder NewString = new StringBuilder(); for (int j = 0; j < result.Length; j++) { string part = RepetingItems(j, result); j += (int)char.GetNumericValue(part\[0\]) - 1; NewString.Append(part); } result = NewString.ToString(); } return result; } string RepetingItems(int index, string array) { int count = 1; for (int i = index + 1; i < array.Length; i++) { if (array\[index\] == array\[i\]) { count++; } else { break; } } return (count.ToString() + array\[index\].ToString()); }
Got your result for the length of 50 iterations, and 100, well... I get an out of memory exception :D. But I do see that the last digits do not change, so I bet I can shave off quite a bit of numbers that I do not need to read. This is way more interesting than reading for the exam, so I should hate you now :D
-
I think I got a working code:
string LookAndSay(int S, int n) { string result = S.ToString(); for (int i = 0; i < (n - 1); i++) { StringBuilder NewString = new StringBuilder(); for (int j = 0; j < result.Length; j++) { string part = RepetingItems(j, result); j += (int)char.GetNumericValue(part\[0\]) - 1; NewString.Append(part); } result = NewString.ToString(); } return result; } string RepetingItems(int index, string array) { int count = 1; for (int i = index + 1; i < array.Length; i++) { if (array\[index\] == array\[i\]) { count++; } else { break; } } return (count.ToString() + array\[index\].ToString()); }
Got your result for the length of 50 iterations, and 100, well... I get an out of memory exception :D. But I do see that the last digits do not change, so I bet I can shave off quite a bit of numbers that I do not need to read. This is way more interesting than reading for the exam, so I should hate you now :D
Want my stick?
-
Want my stick?
Oh yes ;)
-
I think I got a working code:
string LookAndSay(int S, int n) { string result = S.ToString(); for (int i = 0; i < (n - 1); i++) { StringBuilder NewString = new StringBuilder(); for (int j = 0; j < result.Length; j++) { string part = RepetingItems(j, result); j += (int)char.GetNumericValue(part\[0\]) - 1; NewString.Append(part); } result = NewString.ToString(); } return result; } string RepetingItems(int index, string array) { int count = 1; for (int i = index + 1; i < array.Length; i++) { if (array\[index\] == array\[i\]) { count++; } else { break; } } return (count.ToString() + array\[index\].ToString()); }
Got your result for the length of 50 iterations, and 100, well... I get an out of memory exception :D. But I do see that the last digits do not change, so I bet I can shave off quite a bit of numbers that I do not need to read. This is way more interesting than reading for the exam, so I should hate you now :D
:laugh: I ended up doing a brute force method and was surprised at what I ran into along the way and what the final answer was. That's all I'm going to say. :-D
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave Kreskowiak -
:laugh: I ended up doing a brute force method and was surprised at what I ran into along the way and what the final answer was. That's all I'm going to say. :-D
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave KreskowiakThen I'll have to use the harddisk and do IO handling... The pain... :sigh:
-
I think I got a working code:
string LookAndSay(int S, int n) { string result = S.ToString(); for (int i = 0; i < (n - 1); i++) { StringBuilder NewString = new StringBuilder(); for (int j = 0; j < result.Length; j++) { string part = RepetingItems(j, result); j += (int)char.GetNumericValue(part\[0\]) - 1; NewString.Append(part); } result = NewString.ToString(); } return result; } string RepetingItems(int index, string array) { int count = 1; for (int i = index + 1; i < array.Length; i++) { if (array\[index\] == array\[i\]) { count++; } else { break; } } return (count.ToString() + array\[index\].ToString()); }
Got your result for the length of 50 iterations, and 100, well... I get an out of memory exception :D. But I do see that the last digits do not change, so I bet I can shave off quite a bit of numbers that I do not need to read. This is way more interesting than reading for the exam, so I should hate you now :D
-
The numbers were floating out on my desk onto my paper and now it's a real mess here :D
-
It's also known as the Conway Sequence, Look and Say Sequence, and probably some others. It's rather simple. Start with a 1 and then describe what you see for the next iteration. So, starting at 1, the next number is one 1 (11), the next is two 1 (21), then one 2 one 1 (1211), and so on:
1
11
21
1211
111221
312211The question to answer is what's the length in digits of the 100th number in the chain, starting with "1" as the first? The first six numbers have been given above. You could write it out by hand, but I wouldn't recommend it, and as developers, that's not what we do. The seemingly simple challenge is to write the code to come up with the answer. The only hint you get is the 50th number is 894,810 digits long. Oh, and don't bother Googling for code. Those examples will only get you so far and definitely won't get you to the answer.
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave KreskowiakI have a day off, so I thought I would make use of this challenge to try TDD(test-driven development). It's actually a rather good example for TDD and I am finding that writing the tests first is making this challenge a lot faster and even more enjoyable to develop a solution to. Thanks for the challenge :thumbsup: [edit]I started off using stacks to do the calculations and soon ran out of memory. I am now using a less processor intensive method(a secret...) and the application got to line 77 and the length of the line is 1,149,440,192 digits long - flippin' heck! No stack overflow either, as the way I have written the application means that all that will happen is that my disk space will run out - I am not sure if I want to find that out...
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
-
I think I got a working code:
string LookAndSay(int S, int n) { string result = S.ToString(); for (int i = 0; i < (n - 1); i++) { StringBuilder NewString = new StringBuilder(); for (int j = 0; j < result.Length; j++) { string part = RepetingItems(j, result); j += (int)char.GetNumericValue(part\[0\]) - 1; NewString.Append(part); } result = NewString.ToString(); } return result; } string RepetingItems(int index, string array) { int count = 1; for (int i = index + 1; i < array.Length; i++) { if (array\[index\] == array\[i\]) { count++; } else { break; } } return (count.ToString() + array\[index\].ToString()); }
Got your result for the length of 50 iterations, and 100, well... I get an out of memory exception :D. But I do see that the last digits do not change, so I bet I can shave off quite a bit of numbers that I do not need to read. This is way more interesting than reading for the exam, so I should hate you now :D
Yes, these strings get huge, don't they? The whole thing really slows down in a very visible way once the lengths start to reach the 100,000s (40th iteration on) and gets ultra slow from there on in. I'll leave mine running for a while and see exactly where it crashes and burns. Max chars for a StringBuilder are 2,147,483,647, I believe and it's clearly going to hit that at some point so I guess the true answer as you suggest involves hiving off the repeating elements. My code (spectacularly Q&D C# - written on a whim in "do ... while" loops because someone recently said "for" loops are for dinosaurs) looks like this:
static void Main(string\[\] args) { string result = ("1"); int i = 1; do { Console.WriteLine(i.ToString() + "\\t" + result.Length.ToString()); result = GetNextResult(result); i += 1; } while (i < 101); Console.ReadLine(); } static string GetNextResult(string origin) { char character; int occurences; StringBuilder result = new StringBuilder(); do { if (origin.Length == 0) break; ProcessNextGroup(origin, out character, out occurences); origin = origin.Substring(occurences); result.Append(occurences.ToString()); result.Append(character.ToString()); GC.Collect(); } while (true); return result.ToString(); } static void ProcessNextGroup(string sequence, out char character, out int occurences) { int index = 0; character = sequence\[0\]; occurences = 1; do { if (++index > (sequence.Length -1)) break; if (sequence\[index\] == character) occurences += 1; else break; } while (true); } }
98.4% of statistics are made up on the spot.
-
Yes, these strings get huge, don't they? The whole thing really slows down in a very visible way once the lengths start to reach the 100,000s (40th iteration on) and gets ultra slow from there on in. I'll leave mine running for a while and see exactly where it crashes and burns. Max chars for a StringBuilder are 2,147,483,647, I believe and it's clearly going to hit that at some point so I guess the true answer as you suggest involves hiving off the repeating elements. My code (spectacularly Q&D C# - written on a whim in "do ... while" loops because someone recently said "for" loops are for dinosaurs) looks like this:
static void Main(string\[\] args) { string result = ("1"); int i = 1; do { Console.WriteLine(i.ToString() + "\\t" + result.Length.ToString()); result = GetNextResult(result); i += 1; } while (i < 101); Console.ReadLine(); } static string GetNextResult(string origin) { char character; int occurences; StringBuilder result = new StringBuilder(); do { if (origin.Length == 0) break; ProcessNextGroup(origin, out character, out occurences); origin = origin.Substring(occurences); result.Append(occurences.ToString()); result.Append(character.ToString()); GC.Collect(); } while (true); return result.ToString(); } static void ProcessNextGroup(string sequence, out char character, out int occurences) { int index = 0; character = sequence\[0\]; occurences = 1; do { if (++index > (sequence.Length -1)) break; if (sequence\[index\] == character) occurences += 1; else break; } while (true); } }
98.4% of statistics are made up on the spot.
PeejayAdams wrote:
written on a whim in "do ... while" loops because someone recently said "for" loops are for dinosaurs
:laugh:
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Yes, these strings get huge, don't they? The whole thing really slows down in a very visible way once the lengths start to reach the 100,000s (40th iteration on) and gets ultra slow from there on in. I'll leave mine running for a while and see exactly where it crashes and burns. Max chars for a StringBuilder are 2,147,483,647, I believe and it's clearly going to hit that at some point so I guess the true answer as you suggest involves hiving off the repeating elements. My code (spectacularly Q&D C# - written on a whim in "do ... while" loops because someone recently said "for" loops are for dinosaurs) looks like this:
static void Main(string\[\] args) { string result = ("1"); int i = 1; do { Console.WriteLine(i.ToString() + "\\t" + result.Length.ToString()); result = GetNextResult(result); i += 1; } while (i < 101); Console.ReadLine(); } static string GetNextResult(string origin) { char character; int occurences; StringBuilder result = new StringBuilder(); do { if (origin.Length == 0) break; ProcessNextGroup(origin, out character, out occurences); origin = origin.Substring(occurences); result.Append(occurences.ToString()); result.Append(character.ToString()); GC.Collect(); } while (true); return result.ToString(); } static void ProcessNextGroup(string sequence, out char character, out int occurences) { int index = 0; character = sequence\[0\]; occurences = 1; do { if (++index > (sequence.Length -1)) break; if (sequence\[index\] == character) occurences += 1; else break; } while (true); } }
98.4% of statistics are made up on the spot.
No, the problem isnt that the strings are limited. You can load into memory the items you want, and perform the LookNSay from the partial string. When that is read to its end you simply load the next part into memory. Its just a little more complicated that's all:
string LookAndSayFile(int S, int n)
{
//If the string is above this length split it into multiple strings
int SplitStringSize = 2;//Setting the directory where files are stored relativce to the exe file string projectPath = System.IO.Path.GetFullPath(@"..\\..\\..\\..\\"); //Delete files File.Delete(projectPath + "output.txt"); File.Delete(projectPath + "input.txt"); //Add intput and and empty file System.IO.File.WriteAllLines(projectPath + "output.txt", new string\[\] { string.Empty }); System.IO.File.WriteAllLines(projectPath + "input.txt", new string\[\] { S.ToString() }); //Number of iterations you want to perform for (int i = 0; i < (n - 1); i++) { char currentItem = '#'; int count = 1; StringBuilder NewString = new StringBuilder(); using (StreamReader sr = new StreamReader(projectPath + "input.txt")) { while (sr.Peek() >= 0) { string res = sr.ReadLine(); foreach (char j in res) { if (currentItem == '#') { currentItem = j; continue; } if (currentItem == j) { count++; } else { NewString.Append(count.ToString() + currentItem.ToString()); if (NewString.Length > SplitStringSize) { System.IO.File.AppendAllLines(projectPath + "output.txt", new string\[\] { NewString.ToString() }); NewString.Length = 0; } currentItem = j; count = 1; continue
-
Yes, these strings get huge, don't they? The whole thing really slows down in a very visible way once the lengths start to reach the 100,000s (40th iteration on) and gets ultra slow from there on in. I'll leave mine running for a while and see exactly where it crashes and burns. Max chars for a StringBuilder are 2,147,483,647, I believe and it's clearly going to hit that at some point so I guess the true answer as you suggest involves hiving off the repeating elements. My code (spectacularly Q&D C# - written on a whim in "do ... while" loops because someone recently said "for" loops are for dinosaurs) looks like this:
static void Main(string\[\] args) { string result = ("1"); int i = 1; do { Console.WriteLine(i.ToString() + "\\t" + result.Length.ToString()); result = GetNextResult(result); i += 1; } while (i < 101); Console.ReadLine(); } static string GetNextResult(string origin) { char character; int occurences; StringBuilder result = new StringBuilder(); do { if (origin.Length == 0) break; ProcessNextGroup(origin, out character, out occurences); origin = origin.Substring(occurences); result.Append(occurences.ToString()); result.Append(character.ToString()); GC.Collect(); } while (true); return result.ToString(); } static void ProcessNextGroup(string sequence, out char character, out int occurences) { int index = 0; character = sequence\[0\]; occurences = 1; do { if (++index > (sequence.Length -1)) break; if (sequence\[index\] == character) occurences += 1; else break; } while (true); } }
98.4% of statistics are made up on the spot.
Oh, the integer sequence are difined for a bit larger range than Dave gave here: A006751 - OEIS[^]
-
The numbers were floating out on my desk onto my paper and now it's a real mess here :D
:laugh: Not so easy, is it?
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave Kreskowiak -
Oh, the integer sequence are difined for a bit larger range than Dave gave here: A006751 - OEIS[^]
That's cheating! :-D Oh, and what you linked to starts the sequence with a 2, not a 1.
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave Kreskowiak -
It's also known as the Conway Sequence, Look and Say Sequence, and probably some others. It's rather simple. Start with a 1 and then describe what you see for the next iteration. So, starting at 1, the next number is one 1 (11), the next is two 1 (21), then one 2 one 1 (1211), and so on:
1
11
21
1211
111221
312211The question to answer is what's the length in digits of the 100th number in the chain, starting with "1" as the first? The first six numbers have been given above. You could write it out by hand, but I wouldn't recommend it, and as developers, that's not what we do. The seemingly simple challenge is to write the code to come up with the answer. The only hint you get is the 50th number is 894,810 digits long. Oh, and don't bother Googling for code. Those examples will only get you so far and definitely won't get you to the answer.
System.ItDidntWorkException: Something didn't work as expected. C# - How to debug code[^]. Seriously, go read these articles.
Dave KreskowiakStrings? Why not use a List of
Tuple
? :) After more thought and before reading Richard's response...Tuple
Using a byte to implement such a Tuple. Having read Richard's response... Huh, yeah, use a byte to implement aTuple,Tuple>
... :D I would also try to use many threads.