TheAlgorithms-C/math/prime_factoriziation.c

165 lines
3.1 KiB
C
Raw Normal View History

2019-02-12 18:25:18 +03:00
/*
AUTHOR: Christian Bender
DATE: 12.02.2019
DESCRIPTION: This program calculates the prime factoriziation of a positive
integer > 1
2019-02-12 18:25:18 +03:00
*/
#include <assert.h>
2019-02-12 18:25:18 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* initial length of the dynamic array */
#define LEN 10
/* increasing range */
#define STEP 5
/*
this type is for the representation of the prim factoriziation
2019-02-12 18:33:05 +03:00
- its series/range of prime factors
- its length : numbers of prime factors
2019-02-12 18:25:18 +03:00
*/
typedef struct data
{
int *range;
2019-02-12 18:25:18 +03:00
int length;
} range;
typedef range *Range;
2019-02-12 18:25:18 +03:00
2019-02-12 18:33:05 +03:00
/* int_fac : calculates the prime factoriziation of positive integers */
2019-02-12 18:25:18 +03:00
Range int_fact(int);
/* print_arr : prints the integer (heap) array*/
void print_arr(Range);
/* increase : increases the dynamic integer array */
int *increase(int *, int);
2019-02-12 18:25:18 +03:00
/* destroy: destroys the range-structure */
void destroy(Range);
/*
main : simle frame program with a simple UI
*/
int main()
{
int n = 0; /* for user input */
printf("\t\tPrim factoriziation\n\n");
printf("positive integer (> 1) ? ");
scanf("%d", &n);
Range r = int_fact(n);
printf("\nThe factoriziation are: ");
print_arr(r);
destroy(r);
return 0;
}
Range int_fact(int n)
{
assert(n > 1); /* precondition : n must be greater then 1*/
int len = LEN;
int count = 0;
int i = 0;
int *range = (int *)malloc(sizeof(int) * len);
2019-02-12 18:25:18 +03:00
assert(range);
Range pstr = (Range)malloc(sizeof(range));
2019-02-12 18:25:18 +03:00
assert(pstr);
while (n % 2 == 0)
{
n /= 2;
if (i < len)
{
range[i] = 2;
i++;
}
else
{
range = increase(range, len);
len += STEP;
range[i] = 2;
i++;
}
count++;
}
int j = 3;
while (j * j <= n)
2019-02-12 18:25:18 +03:00
{
while (n % j == 0)
{
n /= j;
if (i < len)
{
range[i] = j;
i++;
}
else
{
range = increase(range, len);
len += STEP;
range[i] = j;
i++;
}
count++;
}
j += 2;
}
if (n > 1)
{
if (i < len)
{
range[i] = n;
i++;
}
else
{
range = increase(range, len);
len += STEP;
range[i] = n;
i++;
}
count++;
}
pstr->range = range;
pstr->length = count;
return pstr;
}
void print_arr(Range pStr)
{
assert(pStr); /* checks whether pStr is a null-pointer */
int i = 0;
printf("\n");
for (i; i < pStr->length; i++)
{
if (i == 0)
printf("%d", pStr->range[0]);
else
printf("-%d", pStr->range[i]);
}
printf("\n");
}
int *increase(int *arr, int len)
2019-02-12 18:25:18 +03:00
{
assert(arr); /* checks whether arr is a null-pointer */
int *tmp = (int *)realloc(arr, sizeof(int) * (len + STEP));
2019-02-12 18:25:18 +03:00
assert(tmp);
return tmp;
// assert(arr);
2019-02-12 18:25:18 +03:00
}
void destroy(Range r)
{
free(r->range);
free(r);
}