Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Code for Computing Special Pythagorean Triplet #95

Closed
tsalada opened this issue Sep 28, 2022 · 3 comments
Closed

Incorrect Code for Computing Special Pythagorean Triplet #95

tsalada opened this issue Sep 28, 2022 · 3 comments

Comments

@tsalada
Copy link

tsalada commented Sep 28, 2022

Examples prob009-gerdr.pl and gerdr-feeds.pl give an incorrect answer.

The object is to find three numbers, a < b < c, such that (1) a ** 2 + b ** 2 = c ** 2; and, (2) a + b + c = 1000. The correct answer is 200 375 and 425, but each of the above mentioned scripts gives 31875000 (which is 200 * 375 * 425).

The attached file (adapted from prob009-gerdr.pl) gives the correct answer (thus indicating that the problem stems from using gather ... take).
find_Pythagorean_Triplet.txt

Edit: Full code in the above link is :

#!/usr/bin/env raku

# raku ex_Pythagorean_Triplet_Say.raku

# -------------------------------------------------------------------
#  A Pythagorean triplet is a set of three integers, a < b < c, such
#  that a**2 + b**2 = c**2. Find a triplet where a + b + c = 1000.
# -------------------------------------------------------------------


sub triples($N) {
  for 1..Int((1 - sqrt(0.5)) * $N) -> $a {
    my $u = $N * ($N - 2 * $a);
    my $v = 2 * ($N - $a);

    # If b = u/v is an integer, report triple
    if $u %% $v {
      my $b = $u div $v;
      my $c = $N - $a - $b;
      say "$a $b $c";
    }
  }
}

triples(1000);

# 200 375 425
@JJ
Copy link
Contributor

JJ commented Sep 28, 2022

Good one. Thanks for the report! We'll check it out.

@ngaywood
Copy link

Perhaps change the final line from:
say [*] .list for gather triples(1000);
to:
.say for gather triples(1000);

@tsalada
Copy link
Author

tsalada commented Sep 29, 2022

The solution proposed by ngaywood works on my machine.

In the companion script, prob009-gerdr-feeds.pl, commenting out the line
==> map -> @triple { [*] @triple } \
would yield the correct answer.

JJ added a commit that referenced this issue Jan 22, 2023
JJ added a commit that referenced this issue Jan 22, 2023
JJ added a commit that referenced this issue Jan 22, 2023
@JJ JJ closed this as completed in bb9a353 Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants