SDDSlib
Loading...
Searching...
No Matches
factorize.c
Go to the documentation of this file.
1/**
2 * @file factorize.c
3 * @brief Routines for prime factorization and primality testing.
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author M. Borland, C. Saunders, R. Soliday
14 */
15
16#include "mdb.h"
17
18/**
19 * @brief Determine if a number is prime.
20 *
21 * Checks if the given number is prime.
22 * @param number The number to test.
23 * @return 1 if the number is prime. Otherwise, returns a negative value which is the negative
24 * of the smallest prime factor of the number.
25 */
26int64_t is_prime(int64_t number)
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}
38
39/**
40 * @brief Find the smallest prime factor of a number.
41 *
42 * Returns the smallest prime factor of a given number. If the number is prime, returns the number itself.
43 * @param number The number for which to find the smallest factor.
44 * @return The smallest factor if found, or the number itself if it is prime. If the number is 1, returns 1.
45 */
46int64_t smallest_factor(int64_t number)
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}
56
57/**
58 * @brief Extract the next prime factor from a number.
59 *
60 * Removes and returns the next prime factor from the given number, dividing it out of the number.
61 * Subsequent calls will continue to factorize the updated number.
62 * @param number Pointer to the number to factorize. On return, this value is reduced by dividing out the factor.
63 * @return The next prime factor if any, otherwise 1 if no further factorization is possible.
64 */
65int64_t next_prime_factor(int64_t *number) {
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}
75
76/**
77 * @brief Find the largest prime factor of a number.
78 *
79 * Iteratively factorizes the given number to determine its largest prime factor.
80 * @param number The number to factorize.
81 * @return The largest prime factor of the number, or 1 if the number is 1 or no factorization is possible.
82 */
83int64_t largest_prime_factor(int64_t number) {
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 smallest_factor(int64_t number)
Find the smallest prime factor of a number.
Definition factorize.c:46
int64_t largest_prime_factor(int64_t number)
Find the largest prime factor of a number.
Definition factorize.c:83
int64_t is_prime(int64_t number)
Determine if a number is prime.
Definition factorize.c:26
int64_t next_prime_factor(int64_t *number)
Extract the next prime factor from a number.
Definition factorize.c:65