Advertisement
NikWillOrStuff

Self describing numbers

Dec 11th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. /*
  2. imagine making an explanation/description of a number my counting the amount of each digit in the number
  3. like for example 111123.
  4. the description of this number would be four 1's, one 2, and one 3.
  5. however, you write down this description as a number
  6. 4 1's, 1 2, 1 3. 411213.
  7.  
  8. the goal of this program is to discover numbers where the description of (a number) is the same as the number itself
  9. some examples are:
  10. 22
  11. 23322110
  12. 14333110
  13. */
  14.  
  15. using System;
  16. using System.Collections;
  17.  
  18. namespace Self_Describing_Numbers
  19. {
  20.     class Program
  21.     {
  22.         static void Main(string[] args)
  23.         {
  24.             ArrayList NumbersArray = new ArrayList();
  25.  
  26.             for (int input = 0; input < 100000000; input++)
  27.             {
  28.                 //optimization #2 (skip any input where every 2nd digit is smaller than the last)
  29.                 for (int i = 1; i < input.ToString().Length - 1; i += 2)
  30.                 {
  31.                     int left = int.Parse(input.ToString()[i].ToString());
  32.                     int right = int.Parse(input.ToString()[i + 2].ToString());
  33.  
  34.                     int toAdd = ((left - right) + 1) * (int)Math.Pow(10, input.ToString().Length - (i + 1));
  35.  
  36.                     if (toAdd > 0)
  37.                     {
  38.                         toAdd -= int.Parse(input.ToString().Substring(i + 3));
  39.                         input += toAdd;
  40.                         break;
  41.                     }
  42.                 }
  43.  
  44.                 //optimization #1 (skip any input number with odd amount of digits)
  45.                 if (input.ToString().Length % 2 != 0)
  46.                 {
  47.                     input *= 10;
  48.                 }
  49.  
  50.                 String outputString = "";
  51.  
  52.                 int[] counts = new int[10];
  53.  
  54.                 for (int i = 0; i < input.ToString().Length; i++)//go through every digit of the input number
  55.                 {
  56.                     for (int j = 0; j < 10; j++)//go through all decimal digits
  57.                     {
  58.                         if (input.ToString()[i].ToString() == j.ToString())
  59.                         {
  60.                             counts[j]++;
  61.                         }
  62.                     }
  63.                 }
  64.  
  65.                 for (int j = 0; j < 10; j++)
  66.                 {
  67.                     if (counts[j] == 0)
  68.                         continue;
  69.                     outputString += counts[j];
  70.                     outputString += j;
  71.                 }
  72.                 if (input.ToString() == outputString)
  73.                 {
  74.                     NumbersArray.Add(input.ToString());
  75.                     Console.WriteLine(input.ToString());
  76.                 }
  77.             }
  78.  
  79.             Console.WriteLine("Found numbers:");
  80.             foreach (Object obj in NumbersArray)
  81.                 Console.Write("{0}\n", obj);
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement