Problem 38
- 投稿者 : rei
Original
Take the number 192 and multiply it by each of 1, 2, and 3:
192 1 = 192 192 2 = 384 192 3 = 576By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).
What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, … , n) where n 1?
和訳
192を1, 2, 3で掛けてみよう.
192 × 1 = 192 192 × 2 = 384 192 × 3 = 576積を連結することで1から9のPandigital数 192384576 が得られる. 192384576を 192と(1,2,3)の連結積と呼ぶ.
同じようにして, 9を1,2,3,4,5と掛け連結することでPandigital数918273645が得られる. これは9と(1,2,3,4,5)との連結積である.
整数と(1,2,…,n) (n > 1) との連結積として得られる9桁のPandigital数の中で最大のものを答えよ.
当てにならないソースコード(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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | using System; using System.Collections.Generic; namespace ProjectEuler { class Problem38 : Problem{ public Problem38() { long answer = 0; for (long i=9;i<10000 ; i++) { long pan = ConcatenatedProduct(i); if (IsPandigital(pan) && pan > answer) { answer = pan; Console.WriteLine("{0}:{1}",i,answer); } } Console.WriteLine("> " + answer); } long ConcatenatedProduct(long n) { long pan = n, add, len; for (long i=2;;i++) { add = n * i; len = length(add); if (length(pan) + len <= 9) { pan *= (long)Math.Pow(10, len); pan += add; } else { break; } } return length(pan) == 9 ? pan : 0; } bool IsPandigital(long n) { List<long> digits = new List<long>(); while(n > 0){ long i = n % 10; if (digits.Contains(i) || i==0) { return false; } digits.Add(i); n /= 10; } return true; } int length(long n) { int len = 0; while(n > 0){ ++len; n /= 10; } return len; } } } |

rei@sikios.com
コメントはまだありません。