SDDSlib
Loading...
Searching...
No Matches
factorize.c File Reference

Routines for prime factorization and primality testing. More...

#include "mdb.h"

Go to the source code of this file.

Functions

int64_t is_prime (int64_t number)
 Determine if a number is prime.
 
int64_t smallest_factor (int64_t number)
 Find the smallest prime factor of a number.
 
int64_t next_prime_factor (int64_t *number)
 Extract the next prime factor from a number.
 
int64_t largest_prime_factor (int64_t number)
 Find the largest prime factor of a number.
 

Detailed Description

Routines for prime factorization and primality testing.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file factorize.c.

Function Documentation

◆ is_prime()

int64_t is_prime ( int64_t number)

Determine if a number is prime.

Checks if the given number is prime.

Parameters
numberThe number to test.
Returns
1 if the number is prime. Otherwise, returns a negative value which is the negative of the smallest prime factor of the number.

Definition at line 26 of file factorize.c.

27{
28 int64_t i, n;
29
30 n = sqrt(number * 1.0) + 1;
31 if (n * n > number)
32 n--;
33 for (i = 2; i <= n; i++)
34 if (number % i == 0)
35 return (-i);
36 return (1);
37}

◆ largest_prime_factor()

int64_t largest_prime_factor ( int64_t number)

Find the largest prime factor of a number.

Iteratively factorizes the given number to determine its largest prime factor.

Parameters
numberThe number to factorize.
Returns
The largest prime factor of the number, or 1 if the number is 1 or no factorization is possible.

Definition at line 83 of file factorize.c.

83 {
84 int64_t factor, lastFactor;
85 lastFactor = 1;
86 while ((factor = next_prime_factor(&number)) > 1)
87 lastFactor = factor;
88 return lastFactor;
89}
int64_t next_prime_factor(int64_t *number)
Extract the next prime factor from a number.
Definition factorize.c:65

◆ next_prime_factor()

int64_t next_prime_factor ( int64_t * number)

Extract the next prime factor from a number.

Removes and returns the next prime factor from the given number, dividing it out of the number. Subsequent calls will continue to factorize the updated number.

Parameters
numberPointer to the number to factorize. On return, this value is reduced by dividing out the factor.
Returns
The next prime factor if any, otherwise 1 if no further factorization is possible.

Definition at line 65 of file factorize.c.

65 {
66 int64_t factor;
67 if ((factor = smallest_factor(*number)) > 1) {
68 *number /= factor;
69 while (*number % factor == 0)
70 *number /= factor;
71 return factor;
72 }
73 return 1;
74}
int64_t smallest_factor(int64_t number)
Find the smallest prime factor of a number.
Definition factorize.c:46

◆ smallest_factor()

int64_t smallest_factor ( int64_t number)

Find the smallest prime factor of a number.

Returns the smallest prime factor of a given number. If the number is prime, returns the number itself.

Parameters
numberThe number for which to find the smallest factor.
Returns
The smallest factor if found, or the number itself if it is prime. If the number is 1, returns 1.

Definition at line 46 of file factorize.c.

47{
48 int64_t i;
49
50 if (number == 1)
51 return (1);
52 if ((i = is_prime(number)) == 1)
53 return (number);
54 return (-i);
55}
int64_t is_prime(int64_t number)
Determine if a number is prime.
Definition factorize.c:26