Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- imagine making an explanation/description of a number my counting the amount of each digit in the number
- like for example 111123.
- the description of this number would be four 1's, one 2, and one 3.
- however, you write down this description as a number
- 4 1's, 1 2, 1 3. 411213.
- the goal of this program is to discover numbers where the description of (a number) is the same as the number itself
- some examples are:
- 22
- 23322110
- 14333110
- */
- using System;
- using System.Collections;
- namespace Self_Describing_Numbers
- {
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList NumbersArray = new ArrayList();
- for (int input = 0; input < 100000000; input++)
- {
- //optimization #2 (skip any input where every 2nd digit is smaller than the last)
- for (int i = 1; i < input.ToString().Length - 1; i += 2)
- {
- int left = int.Parse(input.ToString()[i].ToString());
- int right = int.Parse(input.ToString()[i + 2].ToString());
- int toAdd = ((left - right) + 1) * (int)Math.Pow(10, input.ToString().Length - (i + 1));
- if (toAdd > 0)
- {
- toAdd -= int.Parse(input.ToString().Substring(i + 3));
- input += toAdd;
- break;
- }
- }
- //optimization #1 (skip any input number with odd amount of digits)
- if (input.ToString().Length % 2 != 0)
- {
- input *= 10;
- }
- String outputString = "";
- int[] counts = new int[10];
- for (int i = 0; i < input.ToString().Length; i++)//go through every digit of the input number
- {
- for (int j = 0; j < 10; j++)//go through all decimal digits
- {
- if (input.ToString()[i].ToString() == j.ToString())
- {
- counts[j]++;
- }
- }
- }
- for (int j = 0; j < 10; j++)
- {
- if (counts[j] == 0)
- continue;
- outputString += counts[j];
- outputString += j;
- }
- if (input.ToString() == outputString)
- {
- NumbersArray.Add(input.ToString());
- Console.WriteLine(input.ToString());
- }
- }
- Console.WriteLine("Found numbers:");
- foreach (Object obj in NumbersArray)
- Console.Write("{0}\n", obj);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement