Prime Factorization and Related Algorithms

Prime Factor Decomposition

Problem Statement

Develop a functon that accepts a positive integer and outputs all of its prime factors in ascending order, including repeated occurrences. For example, the prime factors of 180 are 2, 2, 3, 3, and 5.

Input Specification

A single positive integer.

Output Specification

All prime factors in ascending order, separated by spaces.

#include <bits/stdc++.h>
using namespace std;

void decompose(int num) {
    for (int divisor = 2; divisor <= sqrt(num); divisor++) {
        if (num % divisor == 0) {
            cout << divisor << " ";
            decompose(num / divisor);
            return;
        }
    }
    if (num > 1) cout << num << " ";
}

int main() {
    int input;
    cin >> input;
    decompose(input);
    return 0;
}

Rounding Floating Point Numbers

Problem Description

Create a program that takes a positive floating-point number and returns the nearest integer. If the fractioanl part is greater than or equal to 0.5, round up; otherwise, round down.

Input Specification

A positive floating-point number.

Output Specification

The rounded integer value.

#include <bits/stdc++.h>
using namespace std;

int main() {
    float value;
    cin >> value;
    cout << static_cast<int>(value + 0.5f) << endl;
    return 0;
}

Merging Records by Key

Problem Statement

Given a list of key-value pairs where keys are indices and values are integers, merge entries with identical keys by summing their values. Output the results sorted by key in ascending order.

Constraints:

  • Index range: 0 <= index <= 11111111
  • Value range: 1 <= value <= 100000

Input Specification

First line: number of records n (1 <= n <= 500) Next n lines: each contains two space-separated integers represanting index and value.

Output Specification

Each merged record on a separate line in the format "index value".

#include <bits/stdc++.h>
using namespace std;

int main() {
    int count;
    cin >> count;
    vector<int> keys;
    unordered_map<int, int> lookup;

    for (int i = 0; i < count; ++i) {
        int key, value;
        cin >> key >> value;
        if (lookup.find(key) == lookup.end()) {
            lookup[key] = value;
            keys.push_back(key);
        } else {
            lookup[key] += value;
        }
    }

    sort(keys.begin(), keys.end());
    for (const auto& k : keys) {
        cout << k << " " << lookup[k] << endl;
    }
    return 0;
}

Extracting Unique Digits from Right to Left

Problem Description

Given an integer, extract digits from right to left and form a new number containing only unique digits. The last digit of the input is guaranteed not to be zero.

Input Specification

A single integer.

Output Specification

An integer formed by taking unique digits from the input in right-to-left order.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int number;
    cin >> number;
    string digits = to_string(number);
    unordered_set<char> seen;
    reverse(digits.begin(), digits.end());
    string result;
    
    for (char digit : digits) {
        if (seen.find(digit) == seen.end()) {
            result += digit;
            seen.insert(digit);
        }
    }
    
    cout << stoi(result) << endl;
    return 0;
}

Note: unordered_set is suitable for storing unique elements while unordered_map handles key-value associations.

Tags: prime-factorization Rounding data-merging unique-digits

Posted on Wed, 28 Oct 2026 16:08:16 +0000 by alwaysme