Skip to content

Instantly share code, notes, and snippets.

@Xmerr
Created December 1, 2016 13:35
Show Gist options
  • Save Xmerr/b0b37226bca7fb14cace645b1933b5d1 to your computer and use it in GitHub Desktop.
Save Xmerr/b0b37226bca7fb14cace645b1933b5d1 to your computer and use it in GitHub Desktop.
Easy Reddit Challenge 290
using System;
using System.Collections.Generic;
namespace Kaprekar_Numbers
{
class Program
{
static void Main(string[] args)
{
string line;
int start, end;
List<int> KaprekarNumbers = new List<int>();
Console.WriteLine("Enter Start and End Numbers to test");
Console.WriteLine("Format like so:");
Console.WriteLine("<Start> <End>\n");
line = Console.ReadLine();
Console.Clear()
for (start = Convert.ToInt32(line.Substring(0, line.IndexOf(' '))), end = Convert.ToInt32(line.Substring(line.IndexOf(' ') + 1));
start <= end; start++)
if (KaprekarTest(start))
Console.Write(start + " ");
Console.WriteLine("");
Console.ReadKey();
}
static bool KaprekarTest(int num)
{
string power = Convert.ToInt64(Math.Pow(num, 2)).ToString();
int num2 = -1;
if(power.Length % 2 == 0)
num2 = Convert.ToInt32(power.Substring(0, power.Length / 2)) + Convert.ToInt32(power.Substring(power.Length / 2));
else if (power.Length > 1)
num2 = Convert.ToInt32(power.Substring(0, (power.Length - 1) / 2)) + Convert.ToInt32(power.Substring((power.Length - 1) / 2));
if (num == num2)
return true;
return false;
}
}
}
@Xmerr
Copy link
Author

Xmerr commented Dec 1, 2016

For Reddit Easy Challenge 290

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment