Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[euler] solution for problem 80
  • Loading branch information
andreoss committed May 19, 2015
1 parent 0ef4319 commit 66ad4a2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
62 changes: 62 additions & 0 deletions categories/euler/prob080-andreoss.pl
@@ -0,0 +1,62 @@
use v6;

=begin pod
=TITLE Square root digital expansion
=AUTHOR Your name (or nick, if you want)
L<https://projecteuler.net/problem=80>
It is well known that if the square root of a natural number is not an integer, then it is irrational.
The decimal expansion of such square roots is infinite without any repeating pattern at all.
The square root of two is 1.41421356237309504880..., and the digital sum of the first one
hundred decimal digits is 475.
For the first one hundred natural numbers, find the total of the digital sums of the first
one hundred decimal digits for all the irrational square roots.
The following algoritms was used for the solution:
L<http://www.afjarvis.staff.shef.ac.uk/maths/jarvisspec02.pdf>
Expected result: 40886
=end pod

use v6;

my constant $limit = 100;

sub sqrt-subtraction($n) {
my Int $a = $n * 5 ;
my Int $b = 5;
while $b < 10 * 10 ** $limit {
given $a <=> $b {
when More | Same {
# replace a with a − b, and add 10 to b.
$a -= $b;
$b += 10;
}
when Less {
# add two zeros to a
$a *= 100;
# add a zero to b just before the final digit (which will always be ‘5’).
$b = ($b - (my $x = $b % 10)) * 10 + $x;
}
}
}
$b;
}

sub MAIN(Bool :$verbose = False) {
say [+] do for 1 ... 100 -> $n {
next if $n.sqrt.floor ** 2 == $n;
my $x = [+] $n.&sqrt-subtraction.comb[^$limit];
say "$n $x" if $verbose;
$x;
}
say "Done in {now - INIT now}" if $verbose;
}



7 changes: 7 additions & 0 deletions t/categories/euler.t
Expand Up @@ -406,7 +406,14 @@ subtest {

check-example-solutions($problem, $expected-output, @authors)
}, "prob067";
subtest {
plan 1;
my $problem = "prob080";
my @authors = <andreoss>;
my $expected-output = 40886;

check-example-solutions($problem, $expected-output, @authors)
}, "prob080";
subtest {
plan 1;

Expand Down

0 comments on commit 66ad4a2

Please sign in to comment.