Skip to content

Instantly share code, notes, and snippets.

@vicenteguerra
Last active December 9, 2016 22:33
Show Gist options
  • Save vicenteguerra/addb8bd94fbc91c6de99faad104c160d to your computer and use it in GitHub Desktop.
Save vicenteguerra/addb8bd94fbc91c6de99faad104c160d to your computer and use it in GitHub Desktop.
/*
* A small college is thinking of instituting a six-digit student ID
* number. It wants to know how many "acceptable" ID numbers there
* are. An ID number is "acceptable" if it has no two consecutive
* identical digits and the sum of the digits is not 7, 11, or 13. *
* 024332 is not acceptable because of the repeated 3s.
* 204124 is not acceptable because the digits add up to 13.
* 304530 is acceptable.
*/
/*
* Function "no_problem_with_digits" extracts the digits from * the ID number from right to left, making sure that there are * no repeated digits and that the sum of the digits is not 7,
* 11, or 13.
*/
#include <stdio.h>
#include <omp.h>
#define NUM_THREADS 1
int no_problem_with_digits (int i)
{
int j;
int latest; /* Digit currently being examined */
int prior; /* Digit to the right of "latest" */
int sum; /* Sum of the digits */
prior = -1;
sum = 0;
for (j = 0; j < 6; j++) {
latest = i % 10;
if (latest == prior)
return 0;
sum += latest;
prior = latest;
i /= 10;
}
if ((sum == 7) || (sum == 11) || (sum == 13))
return 0;
return 1;
}
/*
* Function "main" iterates through all possible six-digit ID
* numbers (integers from 0 to 999999), counting the ones that * meet the college's definition of "acceptable."
*/
int main (void)
{
int count; /* Count of acceptable ID numbers */
int i=0;
double start_time = omp_get_wtime();
omp_set_num_threads(NUM_THREADS); //define el numero de hilos
int nth = omp_get_num_threads();
count = 0;
#pragma omp parallel for
for (i = 0; i< 1000000; i++){
#pragma omp critical
if (no_problem_with_digits(i)) {
count++;
}
}
printf("There are %d acceptable ID numbers\n", count);
printf("Time: \t %f \n", omp_get_wtime()-start_time);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment