a-conjecture-of-mine

An exercise on polyglossy: the same problem solved on multiple languages

commit c328aadfa7925d1325999743f287a24f7f827354
parent bc43e41ee0117bb99f2b34880215a6d926164829
Author: Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date:   Fri, 21 Jun 2019 10:51:52 -0300

Fixed the minor logic error on 'main.c'.

Also renamed variables and functions to make them snake case.

Diffstat:
MC/main.c | 55+++++++++++++++++++++++++++----------------------------
1 file changed, 27 insertions(+), 28 deletions(-)
diff --git a/C/main.c b/C/main.c
@@ -3,7 +3,7 @@
 #include <time.h>
 #include <pthread.h>
 
-// shenanigans to get system information
+// Imports to get system information
 #ifdef _WIN32
 #include <windows.h>
 #elif MACOS
@@ -18,18 +18,19 @@
 // Let S: N -> N be the sum of the digits of a positive integer.
 // For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an integer.
 
-int err = 0;     // global error condition
-int *sums_cache; // shared memory between threads
+int err = 0;     // Global error condition
+int *sums_cache; // Shared memory between threads
 
-// we need a memory structure to pass arguments to threads using the pthreads library
-struct start_max
+// Memory structure to pass arguments to threads using the pthreads library
+struct iter_info
 {
     int start;
+    int interval;
     int max;
 };
 
-// find the number of processors on host machine
-int getNumberOfCores()
+// Find the number of processors on host machine
+int get_num_cores()
 {
 #ifdef WIN32
     SYSTEM_INFO sysinfo;
@@ -59,7 +60,7 @@ int getNumberOfCores()
 #endif
 }
 
-int get_sums(unsigned n)
+int sum_digits(unsigned n)
 {
     int parc = n;
     int sum = 0;
@@ -72,10 +73,10 @@ int get_sums(unsigned n)
     return sum;
 }
 
-int getCounterExample_runner(struct start_max *s_m)
+int get_counterexpl_iter(struct iter_info *iter)
 {
-    for (int a = s_m->start; a <= s_m->max; a++)
-        for (int b = a; b <= s_m->max; b++)
+    for (int a = iter->start; a <= iter->max; a += iter->interval)
+        for (int b = a; b <= iter->max; b++)
         {
             if ((sums_cache[a + b] - (sums_cache[a] + sums_cache[b])) % 9 != 0)
             {
@@ -84,6 +85,7 @@ int getCounterExample_runner(struct start_max *s_m)
                 return 1;
             }
         }
+
     return 0;
 }
 
@@ -91,9 +93,9 @@ int main()
 {
 
     int i;
-    int num_threads = getNumberOfCores();
+    int num_threads = get_num_cores();
     pthread_t thread_ids[num_threads];
-    struct start_max startMax[num_threads];
+    struct iter_info thread_iters[num_threads];
     unsigned int MAX = 0;
 
     printf("\nThis program is a simple test for the following conjecture:\n");
@@ -106,40 +108,37 @@ int main()
     printf("\nLOADING. . ");
     clock_t start = clock(), end;
 
-    // create the sums_cache
+    // Create the sums cache
     int sums_c = 2 * (MAX + 1);
     sums_cache = malloc(sizeof(int) * sums_c * 2); //original (sizeof(int)* sums_c) causing seg faults in multi-threading
     for (i = 0; i <= sums_c; i++)
-        sums_cache[i] = get_sums(i);
+        sums_cache[i] = sum_digits(i);
+        
 
-    // create the threads, divide the task into number of threads equivalent to number
+    // Create the threads, divide the task into number of threads equivalent to number
     // of cores on the host machine
 
     for (i = 0; i < num_threads; i++)
     {
-        startMax[i].start = i * (MAX / num_threads);
-        startMax[i].max = startMax[i].start + (MAX / num_threads) - 1;
-        err = pthread_create(&thread_ids[i], NULL, getCounterExample_runner, &startMax[i]);
-        if (err)
-        {
-            printf("unable to create thread : %d", err);
-        }
+        thread_iters[i].start = i;
+        thread_iters[i].max = MAX;
+        thread_iters[i].interval = num_threads;
+        err = pthread_create(&thread_ids[i], NULL, get_counter_example_iter, &thread_iters[i]);
+        
+        if (err) printf("Unable to create thread : %d", err);
     }
 
     for (i = 0; i < num_threads; i++)
     {
         err = pthread_join(thread_ids[i], NULL);
-        if (err)
-        {
-            printf("unable to join threads :%d\n", err);
-        }
+        if (err) printf("Unable to join threads :%d\n", err);
     }
 
     if (err)
         return 1;
 
     end = clock();
-    printf("\nLOADED. .%d Threads . in %ums\n", num_threads, (unsigned)((end - start) * 1000 / CLOCKS_PER_SEC));
+    printf("\nLOADED. . . in %ums [%d Threads]\n", (unsigned)((end - start) * 1000 / CLOCKS_PER_SEC), num_threads);
     printf("\nThe conjecture is proved for all natural numbers smaller or equals to %u!", MAX);
 
     return 0;