Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[euler] solution for problem 80
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
|
|
||
|
|
||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters