Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- namespace Euler.Palindrome
- {
- [TestClass]
- public class Palindrome
- {
- [TestMethod]
- public void should_be_a_palindrome()
- {
- Convert.ToInt64(9889).IsPalindrome().ShouldBeTrue();
- }
- [TestMethod]
- public void should_still_be_a_palindrome()
- {
- Convert.ToInt64(98089).IsPalindrome().ShouldBeTrue();
- }
- [TestMethod]
- public void should_not_be_a_palindrome()
- {
- Convert.ToInt64(908).IsPalindrome().ShouldBeFalse();
- }
- [TestMethod]
- public void find_it()
- {
- Int64 start = 9999;
- for(Int64 product=Convert.ToInt64(9999*start); product>0;product--)
- {
- if(product.IsPalindrome())
- {
- for (Int64 j = start; j > start-1000;j-- )
- {
- if (product % j == 0 && (product / j).ToString().Length.Equals(3))
- {
- product.Say(j, product / j);
- return;
- }
- }
- }
- }
- }
- }
- public static class extensions
- {
- public static bool IsPalindrome(this Int64 number)
- {
- //var array = number.ToString().ToCharArray();
- //Array.Reverse(array);
- //return array.Equals(number.ToString().ToCharArray());
- var s = number.ToString();
- var len = s.Length;
- for (int i = 0; i < len / 2; i++)
- {
- if (s[i] != s[len - i - 1])
- return false;
- }
- return true;
- }
- public static void ShouldBeTrue(this bool actual)
- {
- Assert.IsTrue(actual);
- }
- public static void ShouldBeFalse(this bool actual)
- {
- Assert.IsFalse(actual);
- }
- public static void Say(this object o, params object[] objects)
- {
- var toSay = string.Empty;
- var things = new[] { o }.Union(objects);
- things.Do(t => toSay += t + " ");
- toSay.Say();
- }
- public static void Do<T>(this IEnumerable<T> source, Action<T> action)
- {
- foreach (var item in source)
- action(item);
- }
- }
- }
Add Comment
Please, Sign In to add comment