Skip to content

Instantly share code, notes, and snippets.

@Flix01
Created August 19, 2024 15:58
Show Gist options
  • Save Flix01/ef372c72f4f292ddd1cc1334b4e96424 to your computer and use it in GitHub Desktop.
Save Flix01/ef372c72f4f292ddd1cc1334b4e96424 to your computer and use it in GitHub Desktop.
Plain C console program to display the manual passages of a multiplication between two (unsigned) numbers
/*
===============================================================================
Public Domain (www.unlicense.org)
===============================================================================
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
/*
WHAT'S THIS:
A plain C console program to display the manual passages of a multiplication between two (unsigned) numbers.
COMPILATION:
gcc -o mult_with_passages mult_with_passages.c
USAGE:
./mult_with_passages 64257 46211
OUTPUT:
64257 *
46211 =
----------------------
64257
642570
12851400
385542000
2570280000
----------------------
2969380227
*/
#include <stdio.h>
#include <assert.h>
unsigned long long multiply_with_passages(unsigned a, unsigned b) {
const int max_number_of_digits_of_the_result = 20; // for printf alignment only
/*if (a < b) { const unsigned tmp = a;a = b;b = tmp; } // (optional) swapping values so that a is bigger is a very small optional optimization */
printf("%*u *\n", max_number_of_digits_of_the_result, a);
printf("%*u =\n", max_number_of_digits_of_the_result, b);
printf("----------------------\n");
// real calculation
unsigned long long result = 0, a_mult = 1UL, b_mult = 1UL;
unsigned a_copy = a, b_copy = b;
while (b_copy) {
unsigned char b_digit = b_copy % 10;
unsigned char carry = 0;
unsigned long long partial = 0;
b_copy /= 10;a_mult = 1UL;
while (a_copy) {
unsigned char a_digit = a_copy % 10;
unsigned char part = a_digit * b_digit + carry;carry = 0;
a_copy /= 10;
if (a_copy && part > 9) { carry = part / 10;part %= 10; }
partial += (unsigned long long)part * (a_mult * b_mult);
a_mult *= 10UL;
}
b_mult *= 10UL;
a_copy = a;
printf("%*llu\n", max_number_of_digits_of_the_result, partial);
result += partial;
}
printf("----------------------\n");
printf("%*llu\n", max_number_of_digits_of_the_result, result);
return result;
}
int main(int argc,char* argv[])
{
unsigned factor1=0,factor2=0;
int rv;unsigned long long result=0;
if (argc<3) {
printf("Usage:\n ./mult_with_passages [positive_number1] [positive_number2]\n");
return -1;
}
rv = sscanf(argv[1], "%u", &factor1);assert(rv==1);
rv = sscanf(argv[2], "%u", &factor2);assert(rv==1);
result = multiply_with_passages(factor1,factor2);
assert(result=(unsigned long long)factor1*(unsigned long long)factor2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment