[2015-05-18] Challenge #215 [Easy] Sad Cycles

Learning C# for my new job.

https://github.com/christarazi/sad-cycles

/*
http://www.reddit.com/r/dailyprogrammer/comments/36cyxf/20150518_challenge_215_easy_sad_cycles/
*/

using System;
using System.Collections.Generic;
using System.Linq;

public class SadCycles
{
    public static void Main()
    {
        List<long> l = new List<long>();

        Console.Write("Enter base: ");
        long baseNum = long.Parse(Console.ReadLine());
        Console.Write("Enter starting number: ");
        string n = Console.ReadLine();

        long sum = 0;
        while (true)
        {
            foreach (char digit in n)
            {
                long temp = digit - '0';
                sum += (long) Math.Pow(temp, baseNum);
            }


            if (l.Where(x => x.Equals(sum)).Count() == 2) break;
            if (sum == 1) break;

            l.Add(sum);
            n = sum.ToString();
            sum = 0;
        }

        List<long> sadCycle = new List<long>();
        foreach (long i in l) 
        {
            if (l.Where(x => x.Equals(i)).Count() > 1) sadCycle.Add(i);
        }

        var sadCycleDistinct = sadCycle.Distinct().ToArray();
        Console.WriteLine("\nSad cycle:\n[" + string.Join(", ", sadCycleDistinct) + "]");
        Console.Write("\nLength of sad cycle: " + sadCycleDistinct.Count() + "\n");
    }
}

Sample output 1:

$ mono sadcycles.exe 
Enter base: 11
Enter starting number: 2

Sad cycle:
[5410213163, 416175830, 10983257969, 105122244539, 31487287760, 23479019969, 127868735735, 23572659062, 34181820005, 17233070810, 12544944422, 31450865399, 71817055715, 14668399199, 134844138593, 48622871273, 21501697322, 33770194826, 44292995390, 125581636412, 9417560504, 33827228267, 21497682212, 42315320498, 40028569325, 40435823054, 8700530096, 42360123272, 2344680590, 40391187185, 50591455115, 31629394541, 63182489351, 48977104622, 44296837448, 50918009003, 71401059083, 42001520522, 101858747, 21187545101, 10669113941, 63492084785, 50958448520, 48715803824, 27804526448, 19581408116, 48976748282, 61476706631]

Length of sad cycle: 48

Sample output 2:

$ mono sadcycles.exe 
Enter base: 3
Enter starting number: 14

Sad cycle:
[371]

Length of sad cycle: 1
/r/dailyprogrammer Thread