Thread: Help with implementing an algorithm!

  1. #31
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Yes, I saw that. We focused on developing the equation of house versus max in earlier posts, I thought EVOEx was talking about the that temp variable. I didn't consider the last part of the code.
    Anyway, I changed it to a long long type. It gave ten results but only the first six were true.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    int isPerfectSquare(unsigned long long );
    int main(int argc, char* argv[]){
                 int count = 0;
                 unsigned long long max = 8;
                 unsigned long long temp;
                 while(count < 10){
                                   temp = (max * (max + 1))/2;
                                   if (isPerfectSquare(temp)){
                                      printf("%.0f\t%d\n",sqrt(temp),max);
                                      count++;
                                   }
                             max++;
                             }
                 printf("\t\t\tEnd of Program\n\t\t\t");
                 system("PAUSE");
                 return 0;
        }
    int isPerfectSquare(unsigned long long i){
                            unsigned long long temp = sqrt(i);
                            return (temp*temp == i)?1:0;
        }
    My output:
    Code:
     6         8
     35        49
     204       288
     1189      1681
     6930      9800
     40391     57121
    92682      131072
    93005      131528
    93418      132113
    94776       134033
    Correct answers:
    Code:
             6         8
            35        49
           204       288
          1189      1681
          6930      9800
         40391     57121
        235416    332928
       1372105   1940449
       7997214  11309768
      46611179  65918161
    Last edited by Leojeen; 09-06-2009 at 03:56 PM.

  2. #32
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok, it works now after I cast the sqrt in the printf to int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. implementing an hours counting algorithm
    By droseman in forum C Programming
    Replies: 6
    Last Post: 02-04-2009, 03:59 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Replies: 2
    Last Post: 10-18-2002, 08:30 AM