Skip to content

Instantly share code, notes, and snippets.

@kunishi
Created July 2, 2012 07:10
Show Gist options
  • Save kunishi/3031628 to your computer and use it in GitHub Desktop.
Save kunishi/3031628 to your computer and use it in GitHub Desktop.
ACM International Collegiate Programming Contest, Japan Domestic, 2006, Problem A
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int main(int argc, char *argv[])
{
FILE *in;
int a, d, n;
int number;
if (argc != 2) {
fprintf(stderr, "usage: %s file\n", argv[0]);
exit(1);
}
if ((in = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "cannot open %s\n", argv[0]);
}
while (1) {
fscanf(in, "%d %d %d", &a, &d, &n);
if (a == 0 && d == 0 && n == 0) {
break;
}
number = a;
while (n != 0) {
if (isPrime(number) == TRUE) {
if (n == 1) {
printf("%d\n", number);
break;
}
n --;
}
number += d;
}
}
}
int isPrime(int x)
{
int i;
if (x == 1) {
return FALSE;
}
for (i = 2; i < x; i ++) {
if (x % i == 0) {
return FALSE;
}
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment