Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
49
Task/Happy-numbers/C-sharp/happy-numbers-1.cs
Normal file
49
Task/Happy-numbers/C-sharp/happy-numbers-1.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace HappyNums
|
||||
{
|
||||
class Program
|
||||
{
|
||||
public static bool ishappy(int n)
|
||||
{
|
||||
List<int> cache = new List<int>();
|
||||
int sum = 0;
|
||||
while (n != 1)
|
||||
{
|
||||
if (cache.Contains(n))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
cache.Add(n);
|
||||
while (n != 0)
|
||||
{
|
||||
int digit = n % 10;
|
||||
sum += digit * digit;
|
||||
n /= 10;
|
||||
}
|
||||
n = sum;
|
||||
sum = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int num = 1;
|
||||
List<int> happynums = new List<int>();
|
||||
|
||||
while (happynums.Count < 8)
|
||||
{
|
||||
if (ishappy(num))
|
||||
{
|
||||
happynums.Add(num);
|
||||
}
|
||||
num++;
|
||||
}
|
||||
Console.WriteLine("First 8 happy numbers : " + string.Join(",", happynums));
|
||||
}
|
||||
}
|
||||
}
|
||||
34
Task/Happy-numbers/C-sharp/happy-numbers-2.cs
Normal file
34
Task/Happy-numbers/C-sharp/happy-numbers-2.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
class Program
|
||||
{
|
||||
|
||||
static int[] sq = { 1, 4, 9, 16, 25, 36, 49, 64, 81 };
|
||||
|
||||
static bool isOne(int x)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (x == 89) return false;
|
||||
int s = 0, t;
|
||||
do if ((t = (x % 10) - 1) >= 0) s += sq[t]; while ((x /= 10) > 0);
|
||||
if (s == 1) return true;
|
||||
x = s;
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
const int Max = 10_000_000; DateTime st = DateTime.Now;
|
||||
Console.Write("---Happy Numbers---\nThe first 8:");
|
||||
int c = 0, i; for (i = 1; c < 8; i++)
|
||||
if (isOne(i)) Console.Write("{0} {1}", c == 0 ? "" : ",", i, ++c);
|
||||
for (int m = 10; m <= Max; m *= 10)
|
||||
{
|
||||
Console.Write("\nThe {0:n0}th: ", m);
|
||||
for (; c < m; i++) if (isOne(i)) c++;
|
||||
Console.Write("{0:n0}", i - 1);
|
||||
}
|
||||
Console.WriteLine("\nComputation time {0} seconds.", (DateTime.Now - st).TotalSeconds);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue