Guest User

Untitled

a guest
Jul 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4.  
  5. namespace Euler.Palindrome
  6. {
  7. [TestClass]
  8. public class Palindrome
  9. {
  10. [TestMethod]
  11. public void should_be_a_palindrome()
  12. {
  13. Convert.ToInt64(9889).IsPalindrome().ShouldBeTrue();
  14. }
  15.  
  16. [TestMethod]
  17. public void should_still_be_a_palindrome()
  18. {
  19.  
  20. Convert.ToInt64(98089).IsPalindrome().ShouldBeTrue();
  21. }
  22.  
  23. [TestMethod]
  24. public void should_not_be_a_palindrome()
  25. {
  26. Convert.ToInt64(908).IsPalindrome().ShouldBeFalse();
  27. }
  28.  
  29. [TestMethod]
  30. public void find_it()
  31. {
  32. Int64 start = 9999;
  33. for(Int64 product=Convert.ToInt64(9999*start); product>0;product--)
  34. {
  35. if(product.IsPalindrome())
  36. {
  37. for (Int64 j = start; j > start-1000;j-- )
  38. {
  39. if (product % j == 0 && (product / j).ToString().Length.Equals(3))
  40. {
  41. product.Say(j, product / j);
  42. return;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49.  
  50. public static class extensions
  51. {
  52. public static bool IsPalindrome(this Int64 number)
  53. {
  54. //var array = number.ToString().ToCharArray();
  55. //Array.Reverse(array);
  56. //return array.Equals(number.ToString().ToCharArray());
  57.  
  58. var s = number.ToString();
  59. var len = s.Length;
  60.  
  61. for (int i = 0; i < len / 2; i++)
  62. {
  63. if (s[i] != s[len - i - 1])
  64. return false;
  65. }
  66. return true;
  67. }
  68.  
  69. public static void ShouldBeTrue(this bool actual)
  70. {
  71. Assert.IsTrue(actual);
  72. }
  73. public static void ShouldBeFalse(this bool actual)
  74. {
  75. Assert.IsFalse(actual);
  76. }
  77. public static void Say(this object o, params object[] objects)
  78. {
  79. var toSay = string.Empty;
  80.  
  81. var things = new[] { o }.Union(objects);
  82.  
  83. things.Do(t => toSay += t + " ");
  84.  
  85. toSay.Say();
  86. }
  87. public static void Do<T>(this IEnumerable<T> source, Action<T> action)
  88. {
  89. foreach (var item in source)
  90. action(item);
  91. }
  92.  
  93. }
  94. }
Add Comment
Please, Sign In to add comment