Help with a curious C problem please

Discussion in 'C' started by adriancdperu, Jul 10, 2008.

  1. adriancdperu

    adriancdperu New Member

    Joined:
    Jul 10, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello world, my name is adrian and im a newbie...

    ]m trying to write a program which tests whether a natural number can be written as the sum of the squares of two not null integers. I am using an auxiliary function to test whether a natural number is a square or not. Seems that compiling errors are produced even by this initial function
    Cant figure out whats wrong though....
    Please somebody help im a big ****....
    thanks in advance
    adrian

    what i did is this:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int sqtest (double n);
    {
    int a;
    if(double sqrt(n)==double floor (sqrt(n)))
    a=1;
    else
    a=0;
    return a;
    }
    
    
    int main ()
    {double n, i, a;
    a=0;
    i=1;
    printf("input n\n");
    scanf("%d",&n);
    while ((i<=double sqrt(n))&&(sqtest(n-i*i)!=0))
    {i=i+1;
    }
    if(i<double sqrt(n))
    printf("n decomposable as sum of squares of:\n" i, double sqrt(n-i*i));
    else
    printf("n indecomposable nontrivially\n");
    return();
    }
    
    ====================================
    figured out that there might be not necessity of putting the "double", so i took them out and the code changed into sth like this:


    Code:
    int sqtest (double n);
    {
    int a;
    if(sqrt(n)==floor (sqrt(n)))
    a=1;
    else
    a=0;
    return a;
    }
    ..........................


    but still it does not work
    can sb help me please im really in a big --------
    Thanks in advance guys! ^^
     
  2. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    I'm not sure why you would want a square root function for the program you've described. To determine if x can be written as a*a+b*b surely you just need a pair of loops, one nested inside the other, to test values of a and b to a suitable maximum, then "if (x==(a*a+b*b)) { /* it can */ }".
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice