-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProb65.pl
More file actions
67 lines (65 loc) · 1.18 KB
/
Prob65.pl
File metadata and controls
67 lines (65 loc) · 1.18 KB
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
57
58
59
60
61
62
63
64
65
66
67
#! usr/bin/perl
#Matthew Digman
use strict;
use warnings;
use bignum;
use Math::BigInt;
use bigint;
my @seq;
my $ans;
for(my $k=1; $k<=50; $k++){
push @seq, (1,2*$k,1);
}
shift @seq;
my @nums=(1);
sub expand{
#print "\n";
my $expr=shift;
my $depth=shift (@_)+1;
my $next=shift @seq;
push @nums, $next;
$expr=~s#/(\d+)(\)*)$#/\($1+1/$next$2#;
my $frac=frac($expr);
$frac=~m#^\d+/#;
my $numerator=$&;
#print "$expr\n $depth $numerator\n";
if($depth<99){
expand($expr,$depth);
}else{
$ans=substr $numerator, 0, (length $numerator)-1;
return 1;
}
1;
}
my $num=0;
sub frac{
my $expr=shift;
#print "expr is $expr\n";
$expr=~s#/(\d+)$##;
my $denom=$1;
#print "denom is $denom\n";
$expr=~s/\+(\d+)$//;
$num=Math::BigInt->new($1);
#print "numerator is $num\n";
$expr=~s#\+1/\((\d+)$##;
my $add=$1;
#print "add is $add\n";
$num+=$add*$denom;
while($expr){
$expr=~s#(\+1/\()?(\d+)$##;
$add=$2;
my $temp=$denom;
$denom=$num;
$num=$temp;
$num=$num+$add*$denom;
#print "numerator is $num\n";
}
"$num/$denom";
}
expand ('2+1/1',1);
sub sum{
my $sum;
$sum+=$_ for(@_);
$sum;
}
print sum split '', $ans;