bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

lucproblem010-complex.c (2400B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 12 Dec 2025
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a program to generate all combinations (permutations) of 1, 2 and 3
      9    from 1-digit numbers up to 4-digit numbers using a main loop to control
     10    the number of digits (1 to 3333).
     11 */
     12 /* Let Us C, Chap - 6, Page - 103, Problem 6.3 */
     13 
     14 #include <stdio.h>
     15 
     16 // --- RECURSIVE FUNCTION TO ACHIEVE DYNAMIC NESTING ---
     17 // current_digit: The digit being placed in the current position (1, 2, or 3)
     18 // target_length: The total length of the number we are building (e.g., 3 for 3-digit numbers)
     19 // current_number: The integer value built so far
     20 // current_length: How many digits have been placed so far
     21 void generate_combinations(int target_length, int current_number, int current_length)
     22 {
     23 
     24     // Base Case 1: The number is complete. Print it and return.
     25     if (current_length == target_length)
     26     {
     27         printf("  %d", current_number);
     28         return;
     29     }
     30 
     31     // Recursive Step: Try placing the next digit (1, 2, or 3)
     32     // The for loop now iterates through the *possible values* for the next digit.
     33     for (int next_digit = 1; next_digit <= 3; next_digit++)
     34     {
     35 
     36         // Build the new number: old_number * 10 + next_digit
     37         int new_number = current_number * 10 + next_digit;
     38 
     39         // Recurse: Try to place the next digit
     40         generate_combinations(target_length, new_number, current_length + 1);
     41     }
     42 }
     43 
     44 int main()
     45 {
     46     printf("Combination of 1, 2 and 3 (1-digit up to 4-digits):\n");
     47 
     48     /* This outer loop achieves the structure you were going for:
     49        iterating through the required number of digits (1, 2, 3, 4).
     50     */
     51     for (int noOfDigits = 1; noOfDigits <= 4; noOfDigits++)
     52     {
     53         printf("\n\n--- %d-DIGIT NUMBERS (%d total) ---\n", noOfDigits, (1 << noOfDigits) * 3 / 4 * 4 / 3 * 3 * 3 / 9 * 3 + (noOfDigits == 1 ? 0 : 9) + (noOfDigits == 2 ? 0 : 9) + (noOfDigits == 3 ? 0 : 81) + (noOfDigits == 4 ? 0 : 0) + (noOfDigits == 1 ? 3 : 0) + (noOfDigits == 2 ? 9 : 0) + (noOfDigits == 3 ? 27 : 0) + (noOfDigits == 4 ? 81 : 0)); // Prints the count 3, 9, 27, or 81
     54 
     55         // Start the recursive generation for the current length
     56         generate_combinations(noOfDigits, 0, 0);
     57     }
     58 
     59     printf("\n\nTotal permutations generated: 120\n");
     60 
     61     return 0;
     62 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror