aboutsummaryrefslogtreecommitdiff
path: root/challenge-021/arne-sommer/perl6/finding-e-fatrat-test
blob: 2a4cd7d41356736aaf316ccab798c45bf88e4632 (plain)
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
#! /usr/bin/env perl6

my $e-seq := gather
{
  take 1;

  my FatRat $current = 1.FatRat;

  for 1 .. Inf
  {
    $current /= $_;
    take $current;
  }
}

sub MAIN (:$steps = 10, :$verbose, :$test)
{
  $verbose && say "{$_ + 1}: { $e-seq[$_].perl }" for ^$steps;

  my $value = $e-seq[^$steps].sum;

  if $test
  {
    my $long = get-euler-from-web;

    print "Answer:  ";
    for ^$value.chars -> $pos
    {
      $value.substr($pos, 1) eq $long.substr($pos, 1)
      ?? print $value.substr($pos, 1)
      !! print "\x1b[41m" ~ $value.substr($pos, 1) ~ "\x1b[0m";
    }
    print "\n";
    say "Correct: " ~ $long.substr(0, $value.chars + 2) ~ "...";

  }
  else
  {
    say $e-seq[^$steps].sum;
  }
}

sub get-euler-from-web
{
  use LWP::Simple;

  my $e-string = "";

  for LWP::Simple.get('http://www-history.mcs.st-and.ac.uk/HistTopics/e_10000.html').lines -> $line
  {
    $e-string ~= $line.trim unless $line ~~ /<[a .. z A .. Z]>/; # Skip lines with html tags
  }

  return $e-string;
}