Thread: quick question passing data types for square root function (function parameters)

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    Cool quick question passing data types for square root function (function parameters)

    hey all i've already worked through the math of this problem and i'm ready to start the code but please i'm just getting 0.000 when I compile this program, I believe it's an issue with the conflicting float and int data types because there are a few. Please help just get the program to output correct squre root for 'c' which is about 31.622 (roughly with %.3f precision) and don't mind all the comments. Problem 9 - Project Euler

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    float square_root (int p);
    
    
    float square_root (int p) // i got this code from Stackoverflow
    {
    	float temp, sqt;
    
    
    	/*
    	printf("Enter number: ");
    	scanf("%d", &n);
    	*/
    
    
    	sqt = p/2;
    	temp = 0;
    
    
    	while (sqt != temp)
    	{
    		temp = sqt;
    		sqt = (p/temp+temp)/2;
    	}
    
    
    	return sqt;
    }
    
    
    int main ()
    {
    	// rule
    	// if hypotonuse is known, divide sides a and b by square root of 2.
    	// sqare root 31.62 * 2
    	// a^2 + b^2 = 1000 = 31.623^2
    	printf("RULE: If hypotenuse is known, divide sides a and by by the square root of 2\n");
    	printf("insert hypotenuse special side value for 'c': \nc: ");
    	//scanf("%f", c); 
    	int c = 1000;
    	int ret;
    
    
    	ret = square_root(c);
    	printf("%.3f\n", ret);
    
    
    	return 0;
    }
    	
    	/* side_a = 31.62/sqrt(2)  side_b =  31.62/sqrt(2); */
    
    
    	/*(
    	while (c != 1)
    	{
    		// convert c (1000) to square root	
    		//root = sqrt(c); // *c is done
    		//printf("%.3f", root);
    		//break;
    		printf("%.3f\n", ret);
    		break;
    		while (c % side_a == 0) // do division inside a while loop 
    		{
    			// divide side_a by square root of 2
    			
    		}
    	}
    	*/

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This line looks wrong I suggest you verify it.

    Code:
    sqt = (p/temp+temp)/2;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    Code:
    float ret
    fixed it

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    If you have an integer type and divide by 2, you'll get an integer value (even if this value is converted, later, to a floating point type), so:
    Code:
    sqt = p/2;
    will always result in an integer. The same happens when you do:
    Code:
    ret = square_root(c);
    Since, ret is also int...

    Anyway... are you sure you implemented Newton & Raphson method correctly?

  5. #5
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    This square root method is wrong (a = 500, b = 500, c = 1000 is not a valid triplet). This is my second attempt. My first attempt seems to be the correct method but I deleted the source code but it was a nested loop.

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    "without goto we would be wtf'd"

  7. #7
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Hey! I've too started solving Project Euler problems very recently and I'm regularly committing to my GitHub repository for Competitive Programming with my solutions. Project Euler is fine with people sharing solutions for the first 100 problems but I'll be making my PE folder private mostly very soon. You can find me and follow my progress here if you'd like to: Competetive-Programming-Q-A/Problems-and-Solutions/Project Euler at master * ZeusNoTZues/Competetive-Programming-Q-A * GitHub

    Also, you could just use sqrt () from math.h instead of writing your own algo to do that.

    Here's my solution to Problem 9. The O(N) solution is worth studying as O(N^3) and O(N^2) are naive/brute-force and easy to think about and work out. Let me know if you need any further explanation or any math proof you would like to see to understand better.

    Code:
    /*
    Solution:
    
    There are many ways to solve this problem. It's a very
    interesting problem due to this.
    There is the obvious O(N^3) approach which can be slightly
    optimised. There's the O(N^2) approach that one can use given
    the relations in the question between A, B, C and doing a little
    math. I managed to solve the problem in these two ways but was
    mind blown when I found that the problem can be solved in with
    an O(N) approach by doing the math further. Thanks to my math
    teacher at school and Wikipedia.
    
    My thoughts:
    
    -> O(N^3):
        var A, B, C : sides of the right angled triangle
        loop A [1-1000]
            loop B [1-1000]
                loop C [1-1000]
                    if (A + B + C equals 1000) and
                       (A^2 + B^2 equals C^2)
                        print A, B, C
    
    This is obviously very lazy and will take a lot of time if the upper
    bound was a larger value than 1000. You can optimise this by using
    the fact that A < B < C is mentioned in the question. You can optimise
    this way more to make it better O(N^3) but I'll just upload the bare
    minimum optimisation code as there are better ways to solve than this.
    N = 1000
    Upper bound for A: N/3
    Upper bound for B: N/2 (and B > A)
    Upper bound for C: N/2 (and C > B)
    
    -> O(N^2)
    Lets's optimise by using the facts given in the question.
    (Note: ^ is not used as XOR here. It denotes "raised to the power of")
    
    --- A + B + C = 1000
    --- A + B = 1000 - C
    --- A^2 + B^2 + 2 * A * B = 1000000 + C^2 - 2000 * C // Squaring both sides
    --- C^2 + 2 * A * B = 1000000 + C^2 - 2000 * C       // As A^2 + B^2 = C^2
    --- 2 * A * B = 1000000 - 2000 * C
    --- A * B = 1000 * (500-C)
    --- A * B = 1000 * (A + B - 500)
    Now, the algorithm depends only upon A and B
    
    -> O(N)
    Magic!
    
    --- A^2 + B^2 = C^2                          // Equation (1)
    --- A^2 + B^2 + 2 * A * B = C^2 + 2 * A * B
    --- (A + B)^2 = C^2 + 2 * A * B
    --- (1000 - C)^2 - C^2 = 2 * A * B           // Equation (2)
    
    (1) - (2):
    --- A^2 + B^2 - 2 * A * B = C^2 - (1000 - C)^2 + C^2
    --- (A - B)^2 = C^2 - 1000000 + 2000 * C
    
    Now, the RHS must be a perfect square as the LHS is a perfect square.
    Hence, we can loop C from N/3 (upper bound for A) to N/2 (upper bound
    for B) and check if RHS is a perfect square.
    
    Good day to you!
    */
    
    #include <bits/stdc++.h>
    
    using namespace std;
    
    // O(N^3)
    
    int main ()
    {
        int Sum, A, B, C;
        cout << "A + B + C = ";
        cin >> Sum;
    
        for (A = 1; A < Sum; A++)
            for (B = 1; B < Sum; B++)
                for (C = 1; C < Sum; C++)
                    if (A < B && B < C && A + B + C == Sum && A*A + B*B == C*C)
                        cout << A << " " << B << " " << C << endl;
    
        return 0;
    }
    
    // O(N^2)
    
    int main ()
    {
        int Sum, A, B;
        cout << "A + B + C = ";
        cin >> Sum;
    
        for (A = 1; A < Sum / 3; A++)
            for (B = 1; B < Sum / 2; B++)
                if (A < B && 2 * A * B == Sum * (Sum - 2 * (Sum - A - B)))
                    cout << A << " " << B << " " << Sum - A - B << endl;
    
        return 0;
    }
    
    // O (N)
    
    int main ()
    {
        int Sum, C;
        cout << "A + B + C = ";
        cin >> Sum;
    
        for (C = Sum / 3; C < Sum / 2; C++) {
            int Value = C * C - Sum * Sum + 2 * Sum * C;
            int sqrt_val = sqrt (Value);
            bool isPerfectSquare = (sqrt_val * sqrt_val == Value);
            if (isPerfectSquare)
            {
                int A = (Sum - C + sqrt_val) / 2;
                int B = Sum - A - C;
                cout << A << " " << B << " " << C << endl;
            }
        }
    
        return 0;
    }
    Last edited by Zeus_; 02-14-2020 at 09:15 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  8. #8
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    interesting,what algo should be used for a ascii endless numbers calculating lots of decimals for sqrt(2) ?which is also like PI= infinite number of decimals
    you tell me you can C,why dont you C your own bugs?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help passing parameters to function
    By RJKinsman in forum C Programming
    Replies: 1
    Last Post: 12-29-2015, 08:19 PM
  2. Square root function compiles but doesn't work
    By SupermanC in forum C Programming
    Replies: 1
    Last Post: 12-05-2013, 07:45 PM
  3. Passing along function parameters
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 16
    Last Post: 12-31-2007, 04:43 AM
  4. Making my own square root function
    By Alexthunder in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2006, 07:36 PM
  5. square root function
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 05-19-2002, 03:11 AM

Tags for this Thread