スキップしてメイン コンテンツに移動

Project Euler - Problem 43

問題

  • 原文

    Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:

    • d2d3d4=406 is divisible by 2
    • d3d4d5=063 is divisible by 3
    • d4d5d6=635 is divisible by 5
    • d5d6d7=357 is divisible by 7
    • d6d7d8=572 is divisible by 11
    • d7d8d9=728 is divisible by 13
    • d8d9d10=289 is divisible by 17

    Find the sum of all 0 to 9 pandigital numbers with this property.

  • 日本語訳

    d1を1桁目, d2を2桁目の数とし, 以下順にdnを定義する. この記法を用いると次のことが分かる.

    • d2d3d4=406は2で割り切れる
    • d3d4d5=063は3で割り切れる
    • d4d5d6=635は5で割り切れる
    • d5d6d7=357は7で割り切れる
    • d6d7d8=572は11で割り切れる
    • d7d8d9=728は13で割り切れる
    • d8d9d10=289は17で割り切れる

    このような性質をもつ0から9のPandigital数の総和を求めよ.

解答

Problem 41と同様にPandigital数を作るだけです。途中で枝切りして無駄な計算を省いています。

除数を保持する配列は一応定数にしてみました。定数の宣言には標準プラグマにconstantがありますが、コードが分かり易いAttribute::ConstantというCPANモジュールを使っています。

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw/say/;
use Attribute::Constant;
use List::Util qw/sum/;
use Set::Object qw/set/;

my @divisors : Constant(2, 3, 5, 7, 11, 13, 17);
sub solutions(;$$);
sub solutions(;$$) {
  my ($num, $rest_digits) = @_;
  $num //= '';
  $rest_digits //= set(0 .. 9);
  my $len = length $num;
  return () if $len >= 4 and substr($num, -3, 3) % $divisors[$len - 4] != 0;
  return () if $num =~ /^0/;
  return ('') if $rest_digits->is_null;

  my @solutions;
  for my $n ($rest_digits->members) {
    $rest_digits->remove($n);
    push @solutions, map { $n . $_ } solutions($num . $n, $rest_digits);
    $rest_digits->insert($n);
  }
  return @solutions;
}

say sum solutions;

コメント