Skip to content

Instantly share code, notes, and snippets.

@xraymemory
Created September 14, 2012 18:34
Show Gist options
  • Select an option

  • Save xraymemory/3723813 to your computer and use it in GitHub Desktop.

Select an option

Save xraymemory/3723813 to your computer and use it in GitHub Desktop.
Program to read in primes from a text file and perform the "switch flipping" problem
#include <stdio.h>
#include <stdlib.h>
#define N 10
int switches[N];
void flipSwitches(int p) {
int j;
int flipped = 0;
for (j = 1; j <= N; j++) {
if (j % p == 0){
if (switches[j] == 0) {
switches[j] = 1;
}
else if (switches[j] == 1){
switches[j] = 0;
}
}
}
}
void main() {
int q;
int w;
for (q = 0; q < N; q++){
switches[q] = 1;
}
FILE* f = fopen("primes.txt", "r");
int prime = 0;
while( fscanf(f, "%d,", &prime) > 0) {
flipSwitches(prime);
}
fclose(f);
long num = 0;
for (w = 0; w < N; w++){
if (switches[w] == 1){
num = num + 1;
}
}
printf("Number of flipped switches is %ld\n", num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment