Skip to content

Instantly share code, notes, and snippets.

@izanbf1803
Created June 11, 2018 20:29
Show Gist options
  • Save izanbf1803/82d0ddf3512536fac83f12c15813f9d3 to your computer and use it in GitHub Desktop.
Save izanbf1803/82d0ddf3512536fac83f12c15813f9d3 to your computer and use it in GitHub Desktop.
Find all perfect primes in range.
#include <stdio.h>
#include <string.h>
#define N 1000000000000000000ULL
short is_prime(unsigned long long n)
{
unsigned long long i;
if (n < 2) return 0;
if (n == 2) return 1;
for (i = 3; i*i <= n; i += 2) {
if (n % i == 0) return 0;
}
return 1;
}
int main()
{
unsigned long long p, k;
for (p = 2; (k = ((1ULL << p) - 1) * (1ULL << (p-1))) <= N; ++p) {
if (is_prime(k)) {
// número perfecto ya que (2^p - 1) * 2^(p-1) es perfecto si (2^p - 1) es primo
printf("%llu\n", k);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment