0
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {

    double a, b, c;

    for (a = 1; a < 333; a++) {
        for (c = 335; c < 998; c++) {
            if ((sqrt(a) != (int) sqrt(a)) || (sqrt(c) != (int) sqrt(c))) continue;
            b = 1000 - c - a;
            if ((c * c) == (a * a) + (b * b)) break;
        }
    }

    printf("%.0lf + %.0lf + %.0lf = 1000\nProduct: %.0lf\n", a, b, c, a * b * c);

    system("pause");
    return 0;
}

I have this part of code whose purpose is to determine if variables a or c have natural square roots. Whenever I run the program I get that value of a and c are their max as written in the for loop, even though their square roots aren't whole numbers. When I assign sqrt(a) or sqrt(c) to different variable I get that for example when a = 332, sqrt(a) = 18.000000 when it should be 18.248288.

idk.mp3
  • 43
  • 6
  • 2
    Turn on warnings. "warning: implicit declaration of function 'sqrt' [-Wimplicit-function-declaration]" – Raymond Chen Jun 16 '23 at 23:10
  • 3
    Did you remember to do `#include `? – Barmar Jun 16 '23 at 23:13
  • @RaymondChen I have turned on, but I don' get any. – idk.mp3 Jun 16 '23 at 23:17
  • @Barmar Yes, I did. – idk.mp3 Jun 16 '23 at 23:17
  • When I do `printf("%d, %.2f\n", (int)a, sqrt(a));` it shows `322 18.22` I don't know why you're getting 18.00 – Barmar Jun 16 '23 at 23:19
  • You have nothing in the `if` block. – Barmar Jun 16 '23 at 23:20
  • 2
    Please post a [mre]. – Barmar Jun 16 '23 at 23:21
  • @Barmar I just tried it and and I get `333, 18.25` but if I compare `sqrt(a) != (int) sqrt(a)` in loop I get false, while outside I get true. – idk.mp3 Jun 16 '23 at 23:27
  • I can't reproduce that. I simplified your code to a single loop with just `a`. I changed the condition to `==` and put `printf("perfect square\n");` inside the `if`. It didn't say that 332 is a perfect square. – Barmar Jun 16 '23 at 23:31
  • Your code never prints anything. What is the expected result? – Barmar Jun 16 '23 at 23:32
  • you have not posted a [mcve]. Something that someone can compile. Take the [tour] and read [ask] please. – OldProgrammer Jun 16 '23 at 23:34
  • The problem is not with `sqrt()` it's with your last `if` statement. – Barmar Jun 16 '23 at 23:35
  • Put a `printf()` after the first `if` statement, you'll see that it detects all the perfect squares correctly. – Barmar Jun 16 '23 at 23:36
  • Where does 1000 come from? – Barmar Jun 16 '23 at 23:37
  • It's from the condition that I was given in the task. – idk.mp3 Jun 16 '23 at 23:41
  • It seems like you're looking for a Pythagorean triple where the values add up to 1000, one of the values is between 1 and 332, another is between 335 and 997. Maybe there aren't any of those. – Barmar Jun 16 '23 at 23:49
  • https://www.reddit.com/r/learnpython/comments/2c5y9w/finding_pythagorean_triple_with_a_sum_of_a_1000/ – Barmar Jun 16 '23 at 23:50
  • 1
    Well I found those limits from math, it's possible I made some mistake. – idk.mp3 Jun 16 '23 at 23:52
  • 1
    Printing with `%.0lf` prints the values with zero decimal places — as an integer. – Jonathan Leffler Jun 17 '23 at 03:45
  • 2
    If `a` is a perfect square between 1 and 333, then the square root of `a` is a number between 1 and 18. In other words, there are only 18 perfect squares between 1 and 333, and they are `1, 4, 9, 16, 25, 36, ...` So write a loop that counts from 1 to 18, and then compute `a` by squaring the loop variable. – user3386109 Jun 17 '23 at 04:31
  • 2
    And you can run the analogous loop for c from 19 up to (not including) 32 – these two loops avoid all the hassle that comes with floating point values, see e.g. [here](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) and [here](https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison), even though these shouldn't be an issue in this very concrete case. Using `double`s as a loop variable has a smell anyway. – Aconcagua Jun 17 '23 at 06:31
  • 1
    You might find [this one](https://godbolt.org/z/xYhhPb36T) pretty interesting… – Aconcagua Jun 17 '23 at 06:58

0 Answers0