Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Functions for Triangle Numbers

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
callmenuge
Cheater
Reputation: 0

Joined: 12 Feb 2011
Posts: 42

PostPosted: Wed Mar 16, 2011 6:19 pm    Post subject: Functions for Triangle Numbers Reply with quote

I made these two functions for problem 12 on Project Euler and thought it would be interesting to see the difference in the calculation times. Can someone benchmark these two functions for me? Another interesting note that is irrelevant to the discussion at hand: for any other IB student that may be browsing these forums, this question was my calculus BC IA topic.

Code:
/* series */

int recursion(int z)
{
   int s = 0;
   
   for(int r = z; r > 0; r--)
   {
      s += r;
   }
   
   return s;
}

/* sequence */

int iteration(int z)
{
   return z * (z + 1) / 2;
}
Back to top
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Thu Mar 17, 2011 10:19 pm    Post subject: Reply with quote

What is there to say? there's a formula to calculate the Nth triangle number in O(1) - constant time, unlike the other function which calculates the number in O(n) operations.

The functions names are confusing because they're wrong - "recursion" isn't recursive and "iteration" has only one iteration. o.o

Regarding to problem 12 - I think you shouldn't use a function. as you can see, besides the formula (which includes multiply and division - quite expensive operations) there's another way to represent a triangle number.

a0 = 1
ai+1 = ai+1 + di

d0 = 1
di+1 = di + 1

so you can calculate the next triangle number with two simple operations - addition.

A simple solution, but not very inefficient would look something like this:
Code:
int t = 0, d = 1;
do
{
    t += d;
    d++;
} while (count_divisors(t) < 500);

return t;


This problem isn't that difficault - so you don't even really need such a good count_divisors function. a simple loop to the square root should be fast enough, something like this:
Code:
int count_divisors(int n)
{
    int count = 2; //1 and n
    int sqrt = (int)sqrt(n);
    int odd = n & 1;

    for (int i = 2 + odd; i < sqrt; i += 1 + odd)
    {
        if (n % i == 0)
            count += 2; //n % i == 0 && n % (n / i) == 0
    }

    if (n % sqrt == 0) //square number
        count++;

    return count;
}




Edit: I wrote this code:
Code:
#include <stdio.h>
#include <math.h>

int count_divisors(int n)
{
    int count = 2; //1 and n
    int sqroot = (int)sqrt((double)n);
    int odd = n & 1;

    for (int i = 2 + odd; i < sqroot; i += 1 + odd)
    {
        if (n % i == 0)
            count += 2; //n % i == 0 && n % (n / i) == 0
    }

    if (n % sqroot == 0) //square number
        count++;

    return count;
}

int problem12()
{
   int n = 0, d = 1;
    do
   {
        n += d;
      d++;
   } while (count_divisors(n) < 500);

   return n;
}

int main()
{
   printf("%d\n", problem12());

    return 0;
}

I changed the name of the variable "sqrt" to "sqroot" because there was a function named sqrt... I wrote the previous code in the browser. Razz

_________________
SharpDisassembler

"When I find my code in tons of trouble,
Friends and colleagues come to me...
Speaking words of wisdom:
Write in C."


#pragma message("Let there be byte!")
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites