Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Recursive Algorithm Implementations in C++

Tech May 17 2

Collatz Conjecture Verification

The Collatz conjecture states that for any positive integer, if it's even divide by 2, if it's odd multiply by 3 and add 1, eventually the sequence will reach 1. This program calculates the number of steps required.


#include <iostream>
using namespace std;

int collatzSteps(int num) {
    if (num == 1) return 0;
    if (num % 2 == 0) return 1 + collatzSteps(num / 2);
    return 1 + collatzSteps(num * 3 + 1);
}

int main() {
    int input;
    cin >> input;
    cout << collatzSteps(input);
    return 0;
}
</iostream>

Greatest Common Divisor Calculation

This program calculates greatest common divisor of two numbers using Euclidean algorithm.


#include <iostream>
using namespace std;

int computeGCD(int x, int y) {
    if (x % y == 0) return y;
    int remainder = x % y;
    return computeGCD(y, remainder);
}

int main() {
    int first, second;
    cin >> first >> second;
    cout << computeGCD(first, second);
    return 0;
}
</iostream>

Number Generation Count

Given a natural number n, count how many new numbers can be generated by repeatedly adding numbers to the left that don't exceed half of the current number.


#include <iostream>
using namespace std;

int countNumbers(double current, int total) {
    if (current == 2) return 1;
    if (current == 1) return 0;
    if (current == 3) return 1;
    
    for (int i = 1; i <= current / 2; i++) {
        total++;
        total = total + countNumbers(i, 0);
    }
    return total;
}

int main() {
    double num;
    cin >> num;
    cout << countNumbers(num, 0);
    return 0;
}
</iostream>

Apple Distribution Problem

Calculate the number of ways to distribute M identical apples into N identical plates, allowing empty plates.


#include <iostream>
using namespace std;

int distributionCount(int apples, int plates) {
    if (apples == 1 || plates == 1 || apples == 0 || plates == 0) return 1;
    if (apples < 0) return 0;
    if (apples < plates) return apples;
    return distributionCount(apples - plates, plates) + distributionCount(apples, plates - 1);
}

int main() {
    int testCases;
    cin >> testCases;
    int appleCount[50], plateCount[50];
    
    for (int i = 0; i < testCases; i++) {
        cin >> appleCount[i] >> plateCount[i];
    }
    
    for (int i = 0; i < testCases; i++) {
        cout << distributionCount(appleCount[i], plateCount[i]) << endl;
    }
    return 0;
}
</iostream>

Digital Root Calculation

Calculate the digital root of a number by repeatedly summing its digits until a single digits obtained.


#include <iostream>
using namespace std;

int digitalRoot(int value) {
    if (value / 10 == 0) return value;
    int digitSum = 0;
    int temp = value;
    
    while (temp != 0) {
        digitSum = digitSum + temp % 10;
        temp = temp / 10;
    }
    return digitalRoot(digitSum);
}

int main() {
    int number;
    cin >> number;
    cout << digitalRoot(number);
    return 0;
}
</iostream>

Ackermann Function

Implementation of the Ackermann function, a classic example of a recursive function that grows very rapidly.


#include <iostream>
using namespace std;

int ackermann(int m, int n) {
    if (m == 0) return n + 1;
    if (m > 0 && n == 0) return ackermann(m - 1, 1);
    if (m > 0 && n > 0) return ackermann(m - 1, ackermann(m, n - 1));
}

int main() {
    int first, second;
    cin >> first >> second;
    cout << ackermann(first, second);
    return 0;
}
</iostream>

Palindrome Formation Steps

Calculate the minimum number of operations needed to make a number palindrome by repeatedly adding its reverse.


#include <iostream>
using namespace std;

int palindromeSteps(int number, int stepCount) {
    int reversed = 0;
    int temp = number;
    
    while (temp != 0) {
        reversed = reversed * 10 + temp % 10;
        temp = temp / 10;
    }
    
    if (reversed == number) return stepCount;
    
    int sum = reversed + number;
    temp = sum;
    reversed = 0;
    
    while (temp != 0) {
        reversed = reversed * 10 + temp % 10;
        temp = temp / 10;
    }
    
    stepCount++;
    if (reversed == sum) return stepCount;
    return stepCount + palindromeSteps(sum, 0);
}

int main() {
    int input;
    cin >> input;
    cout << palindromeSteps(input, 0);
    return 0;
}
</iostream>

Least Common Multiple Calculation

Calculate the least common multiple of two numbers using their greatest common divisor.


#include <iostream>
using namespace std;

int num1, num2;

int findLCM(int a, int b) {
    if (a % b == 0) return (num1 * num2) / b;
    int remainder = a % b;
    return findLCM(b, remainder);
}

int main() {
    cin >> num1 >> num2;
    cout << findLCM(num1, num2);
    return 0;
}
</iostream>

Hermite Polynomial Calculation

Calculate Hermite polynomials using recursive relation.


#include <iostream>
using namespace std;

int hermite(int degree, int variable) {
    if (degree == 0) return 1;
    if (degree == 1) return 2 * variable;
    if (degree > 1) return 2 * variable * hermite(degree - 1, variable) - 2 * (degree - 1) * hermite(degree - 2, variable);
}

int main() {
    int n, x;
    cin >> n >> x;
    cout << hermite(n, x);
    return 0;
}
</iostream>
Tags: RecursionC++

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.