- Edited
could this be used in C# too? or the only way is to save the sums in a list?arithma wroteThe idea is simple. If you have a loop, two operations per step versus one operation per step will collide with each other.
This eliminates the need to store or memorize what happened before to compare against.
btw here's my C# version:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
uint n, s = 0;
bool b=false;
Console.Write("Enter a positif number to find out it's mood:");
n = uint.Parse(Console.ReadLine());
List<uint> li=new List<uint>();
while(n!=1 && b==false)
{
li.Add(n);
while (n != 0)
{
s += (n % 10) * (n % 10);
n /= 10;
if (li.Contains(n)) { b = true; }
}
n = s;
s = 0;
}
if (b == true) Console.WriteLine("sad");
else Console.WriteLine("happy");
Console.ReadLine();
}
}
}