Problem26

30分プログラム、その297。Problem26 via ProjectEuler
解けたっ。自前で小数点の計算をして、循環を検出するようにした。1/983は983もの長さの循環節を持つらしいんだけど本当かよ。

使い方

$ time perl problem26.pl
.....
887 : 886
937 : 936
941 : 940
953 : 952
971 : 970
977 : 976
983 : 982
perl problem26.pl  10.15s user 0.24s system 86% cpu 11.944 total

ソースコード

#! /usr/bin/perl
# -*- mode:perl; coding:utf-8 -*-
#
# problem26.pl -
#
# Copyright(C) 2008 by mzp
# Author: MIZUNO Hiroki / mzpppp at gmail dot com
# http://howdyworld.org
#
# Timestamp: 2008/05/05 20:33:02
#
# This program is free software; you can redistribute it and/or
# modify it under MIT Lincence.
#
use strict;
use warnings;
use integer;
use List::MoreUtils qw(first_index);

no warnings "recursion";

sub fdiv($$@){
    my ($a,$b,@nums) = @_;

    my $i = first_index { $_ == $a } @nums;
    if($i != -1){
	return $i+1;
    }

    my @new_nums = ($a,@nums);
    my $r = $a % $b;

    if($a < $b){
	&fdiv(10*$a,$b,@new_nums);
    }elsif($r == 0){
	0;
    }else{
	&fdiv($r*10,$b,@new_nums);
    }
}

my $max=0;
for (1..999){
    my $n = fdiv(1,$_);
    if($max < $n){
	print "$_ : $n\n";
	$max = $n;
    }
}