Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Implementation of the "9" algorithm
- * The algorithm is simple: take any number > 9, and add its digits together, then subtract the sum from the number.
- * Like the Collatz Conjecture, this will always take you to a certain number (in this case: 9).
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int
- get_sum(int number)
- {
- int total = 0;
- while(number){
- total += number % 10;
- number /= 10;
- }
- return total;
- }
- void
- transform_loop(char *initial)
- {
- for(int number = atoi(initial); number >= 9; number -= get_sum(number))
- printf("%i (sum = %i)\n", number, get_sum(number));
- printf("0\n----\n");
- }
- void
- parse_args(char **argv)
- {
- while(*argv)
- transform_loop(*(argv++));
- }
- void
- usage()
- {
- fputs("Usage: 9 n1 [n2 n3 ...]\n", stderr);
- exit(1);
- }
- int
- main(int argc, char **argv)
- {
- if(!argv[1])
- usage();
- parse_args(argv + 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement