thoga31

Primality of numbers of the form 2n^2-1 (Euler, 216)

Aug 27th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. /* ### Project Euler, problem 216 ### */
  2. /* Notice that I'm not the best C programmer ever :D */
  3.  
  4. #include <stdio.h>
  5. #include <tgmath.h>
  6.  
  7. #define MAX 50000000
  8.  
  9. short isPrime(unsigned long n) {
  10.     int i;
  11.     for(i=2; i<=(unsigned long) trunc(sqrt((double) n)); ++i)
  12.         if (n % i == 0) return 0;
  13.     return 1;
  14. }
  15.  
  16. unsigned long t(unsigned long n){
  17.     return 2 * n*n - 1;
  18. }
  19.  
  20. int main(void) {
  21.     unsigned long i, count=0;
  22.     for(i=2; i<=MAX; ++i)
  23.         if (isPrime(t(i)))
  24.             ++count;
  25.     printf("%lu", count);
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment