Problem 4
- 2010年 1月 24日
- 投稿者 : rei
Original
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
和訳
左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、最大のものは 9009 = 91 × 99 である。
では、3桁の数の積で表される回文数のうち最大のものはいくらになるか。
当てにならないソースコード(C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; namespace ProjectEuler { class Problem4 { public Problem4() { int x, y, num = 0, answer = 0; Console.WriteLine(this.ToString()); for (x = 999; x > 0; x--) { for (y = 999; y > 0; y--) { num = x * y; if (num <= answer) break; if (num == Reverse(num)) { answer = num; break; } } } Console.WriteLine("> " + answer); } int Reverse(int num) { string s = num.ToString(); int i = s.Length; char[] chs = new char[i]; foreach (char c in s) chs[--i] = c; return int.Parse(new string(chs)); } } } |
たいした数でもないので全て走査しても一瞬で済みますが、
もっと数が大きくなると無駄を省く必要がありそうです。
コメントはまだありません。