Compare commits
5 Commits
problem-40
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0551709888 | ||
|
|
fe204db23c | ||
|
|
a669b56c13 | ||
|
|
a73040bc67 | ||
|
|
fe22240e29 |
27
primes.h
27
primes.h
@@ -1,3 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
int is_prime(long num)
|
||||
{
|
||||
int i = 5;
|
||||
@@ -19,3 +24,25 @@ int is_prime(long num)
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* eratosthenes(long num)
|
||||
{
|
||||
long i, j;
|
||||
char* primes;
|
||||
|
||||
primes = (char*)malloc((num+1)*sizeof(char));
|
||||
if (primes == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(primes, TRUE, num);
|
||||
primes[1] = FALSE;
|
||||
|
||||
for (i=2; i<=num; i++)
|
||||
if (primes[i])
|
||||
/* Mark factors as composite */
|
||||
for (j=i*i; j<num; j+=i)
|
||||
primes[j] = FALSE;
|
||||
|
||||
return primes;
|
||||
}
|
||||
|
||||
61
problem-112.c
Normal file
61
problem-112.c
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
int is_increasing(long num)
|
||||
{
|
||||
int digit;
|
||||
int prev = num % 10;
|
||||
num /= 10;
|
||||
|
||||
while (num>0) {
|
||||
digit = num % 10;
|
||||
if (digit>prev)
|
||||
return FALSE;
|
||||
num /= 10;
|
||||
prev = digit;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
int is_decreasing(long num)
|
||||
{
|
||||
int digit;
|
||||
int prev = num % 10;
|
||||
num /= 10;
|
||||
|
||||
while (num>0) {
|
||||
digit = num % 10;
|
||||
if (digit<prev)
|
||||
return FALSE;
|
||||
num /= 10;
|
||||
prev = digit;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
int is_bouncy(long num)
|
||||
{
|
||||
if (is_increasing(num) || is_decreasing(num))
|
||||
return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int i = 99;
|
||||
int count = 0;
|
||||
|
||||
while((count*100) != (i*99)) {
|
||||
i++;
|
||||
if (is_bouncy(i))
|
||||
count++;
|
||||
}
|
||||
|
||||
printf("The solution is %d / %d = %.9f%%\n", count, i, (double)count/(double)i);
|
||||
return 0;
|
||||
}
|
||||
39
problem-206.c
Normal file
39
problem-206.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
#define MIN_NUM 1020304050607080900
|
||||
#define MAX_NUM 1929394959697989990
|
||||
|
||||
int digits[] = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1};
|
||||
|
||||
int check(long num)
|
||||
{
|
||||
int rem;
|
||||
int i = 0;
|
||||
|
||||
while (num>0) {
|
||||
rem = num % 10;
|
||||
if (rem != digits[i])
|
||||
return FALSE;
|
||||
num /= 100;
|
||||
i++;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
long i = 1010101011; /* while ((i*i) < MIN_NUM) i++; */
|
||||
|
||||
while ((i*i) < MAX_NUM) {
|
||||
if (check(i*i))
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
printf("The unique integer is %ld and it's square is %ld\n", i, i*i);
|
||||
return 0;
|
||||
}
|
||||
53
problem-357.c
Normal file
53
problem-357.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "primes.h"
|
||||
|
||||
#define MAX_NUM 100000000
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, match, midpoint;
|
||||
long sum = 0;
|
||||
char *primes;
|
||||
|
||||
/*
|
||||
primes = malloc((MAX_NUM+1)*sizeof(char));
|
||||
for (i=1; i<MAX_NUM+1; i++) {
|
||||
if (is_prime(i)) {
|
||||
primes[i] = TRUE;
|
||||
} else {
|
||||
primes[i] = FALSE;
|
||||
}
|
||||
}
|
||||
*/
|
||||
primes = eratosthenes(MAX_NUM);
|
||||
if (primes == NULL) {
|
||||
printf("Error");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for(i=1; i<MAX_NUM; i++) {
|
||||
match = TRUE;
|
||||
if (!(primes[i+1])) continue;
|
||||
|
||||
midpoint = (int)sqrt(i) + 1;
|
||||
for (j=1; j<midpoint; j++) {
|
||||
if ((!(i%j)) && (!primes[j+i/j])) {
|
||||
match = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (match) {
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("The sum of prime-generating numbers less than %d is %ld\n", MAX_NUM, sum);
|
||||
free(primes);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user