Menu
Coddy logo textTech

Prime factors

Lesson 7 of 20 in Coddy's Mathematical Riddles course.

Writing a number as a product of prime numbers is called a prime factorization of the number.

For example:

13195 = 5 * 7 * 13 * 29

The terms in the product are called prime factors.

challenge icon

Challenge

Easy

Write function calcPrimeFactors which gets a number (integer) and returns list of the prime factors of the given number.

The primes in the list must be ordered from low to high!

Try it yourself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "solution.h"

int main() {
    int n;
    if (scanf("%d", &n) != 1) n = 0;
    int rs = 0;
    int* r = calcPrimeFactors(n, &rs);
    for (int i = 0; i < rs; i++) {
        if (i > 0) printf(" ");
        printf("%d", r[i]);
    }
    printf("\n");
    return 0;
}

All lessons in Mathematical Riddles

4Primes

Is prime?Prime factors

7Least common multiple

IntroductionA problem