Skip to content

Instantly share code, notes, and snippets.

@choupi
Created July 6, 2014 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choupi/35b943188c986d8dd2d3 to your computer and use it in GitHub Desktop.
Save choupi/35b943188c986d8dd2d3 to your computer and use it in GitHub Desktop.
find max n such that s <= LONG_MAX, for s=1+2+3+...+n (O(1) algorithm)
#include <stdio.h>
#include <limits.h>
#include <math.h>
int main()
{
double nn=(1+sqrt(1+4.0*LONG_MAX*2.0))/2.0;
long int n= nn, s=n/2*(n-1),t;
printf("%lf %lf\n", nn, log(nn)/log(2.0));
printf("%ld %ld %ld\n", n, s, s+n+1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment