Skip to content

Instantly share code, notes, and snippets.

@threecee
Last active August 29, 2015 14:10
Show Gist options
  • Save threecee/a203d5f0ea965f6b1b33 to your computer and use it in GitHub Desktop.
Save threecee/a203d5f0ea965f6b1b33 to your computer and use it in GitHub Desktop.
Julekalender 2014
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#include <sys/stat.h>
bool primes[50];
void fillSieve() {
primes[0] = primes[1] = true;
for (int i = 2; i < 50; i++) {
if (!primes[i]) {
for (int j = 2; i * j < 50; j++) {
primes[i * j] = true;
}
}
}
}
bool isPrime(int n) {
return !(primes[n]);
}
int getPerfectNumber(int prime) {
return (int) (((2 << (prime - 2))) * ((2 << (prime-1)) - 1));
}
int main() {
int LIMIT = 10000;
fillSieve();
for (int i = 2; i <= LIMIT; i++) {
if (isPrime(i)) {
int x = getPerfectNumber(i);
if (x <= LIMIT) {
printf("%d,", x);
} else {
return 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment