A Simple Algorithm For Prime Factorizati
A Simple Algorithm For Prime Factorizati
Journal of Mathematics
Volume 2022, Article ID 7034529, 10 pages
https://doi.org/10.1155/2022/7034529
Research Article
A Simple Algorithm for Prime Factorization and Primality Testing
1,2
Kabenge Hamiss
1
Department of Mathematics and Statistics, Islamic University in Uganda, Mbale 2555, Uganda
2
Department of Mathematical and Computer Sciences, University of Gezira, Wad Medani 20, Sudan
Received 1 September 2022; Revised 31 October 2022; Accepted 7 November 2022; Published 15 December 2022
Copyright © 2022 Kabenge Hamiss. Tis is an open access article distributed under the Creative Commons Attribution License,
which permits unrestricted use, distribution, and reproduction in any medium, provided the original work is properly cited.
We propose a new simple and faster algorithm to factor numbers based on the nature of the prime numbers contained in such composite
numbers. It is well known that every composite number has a unique representation as a product of prime numbers. In this study, we focus
mainly on composite numbers that contain a product of prime numbers that are greater than or equal to 5 which are of the form 6k + 1 or
6k + 5. Terefore, we use the condition that every prime or composite P of primes greater than or equal to 5 satisfes P2 ≡ 1 (mod24).
Tis algorithm is very fast especially when the diference in the prime components of a composite number (prime gap) is not so large.
When the diference between the factors (prime gap) is not so large, it often requires just a single iteration to obtain the factors.
p1 1 ≡ 1 (mod24),
2k m � 6k + 5. (14)
p2 2 ≡ 1 (mod24), . . . ,
2k Equations (10), (12), and (14) really show that not all
(5)
numbers of the form 6k + 1 or 6k + 5 are primes. Some of
n ≡ 1 (mod24).
p2kn
which are pseudoprimes and others just ordinary odd
numbers.
From the property in [7] of multiplication, then mul• As we can observe from the above mentioned equations,
tiplying p2i in (5) together, we obtain the values of r and t will determine whether the number is
prime or not a prime.
n ≡ 1 (mod24).
2k 2k
p1 1 p2 2 . . . p2k n (6)
We also observe that it is difcult to distinguish between
Multiplying s2k both side in (6), we obtain composites of the form (6t + 1)(6r + 1) and (6t + 5)(6r + 5)
because they both give the same result, that is, 6k + 1.
n ≡ s (mod24).
2k 2k
s2k p1 1 p2 2 . . . p2k n 2k
(7)
2k 2k 2kn
However, m2 � s2k p1 1 p2 2 . . . pn implying 2.3. Composites of the Form 6k + 1 and Teir Factorization
m2 ≡ s2k (mod24). (8) Case 3. P � (6t + 1)(6r + 1).
Tus, the theorem is proven.
Tis indicates that any composite number that contains Theorem 2. Let
at least one prime less than 5 does not satisfy the condition of P � (6t + 1)(6r + 1). (15)
1 (mod24).
Terefore, from this point, we can analyze the conditions Ten, there exists some integer A such that
that will determine any integer to be a prime or a
A ≡ (r + t) (mod6). (16)
composite. □
Proof. From
2.1. Composite of the Form 6k + 1. As stated earlier, not all P � (6t + 1)(6r + 1) � 6[6tr +(r + t)] + 1. (17)
numbers of the form 6k + 1 are primes, now, we prove to
back up the statement. Ten,
P − 1 � 6[6tr + (r + t)]. (18)
Case 1. Let m be a composite of at least two prime say of the
form 6t + 1 and 6r + 1, that is, m � (6t + 1)(6r + 1): Dividing through by 6, we obtain
m � 36tr + 6(r + t) + 1, P− 1
(9) � (r + t) + 6rt. (19)
m � 6[6rt +(r + t)] + 1. 6
Journal of Mathematics 3
Now, let A � (P − 1/6) and u � rt implying that 20 2.4. Factorizing the Composite of Case 3. Suppose P � (6t +
becomes 1)(6r + 1) is a composite with at least two prime factors;
then, there exists some positive integer A such that A ≡ t +
A � (r + t) + 6u. (20)
r (mod6) as in (17). Let
Hence, we obtain t + r � ao + 6f, (27)
A ≡ (r + t) (mod6). (21)
□ such that A − ao is divisible by 6, where f � 0, 1, 2, . . . , n
and ao is the smallest positive residue. Ten,
Lemma 1. If r + t � A, then either r � A, t � 0 or r � 0, t � rt � B − f, (28)
A is a solution (trivial solution) to (16).
where B � (A − ao /6).
From (28) and (29), we obtain
Proof. Let r + t � A. We know that u � tr � 0. Ten, it
implies that r � A − t: t2 − ao + 6ft +(B − f) � 0. (29)
Corollary 1. Let P be a number of the form 6k + 1 for any 36f2 + 12ao + 4f − 4B + a2o ≥ 0. (31)
integer k ≥ 1; then, for some P is a prime, if and only if A � k
and 0 (trivial solution) is the only solution to (16). Ten, let h be the least positive integer solution for f
satisfying (32). Ten, fn � h + n, where n � 0, 1, . . . , η. Tis
implies that fmin � h for the real solution in (30).
Proof. P � 6k + 1 can be written in the form (6t + 1)(6r +
Substituting fn � h + n in (30), we obtain
1) and clearly; if A and 0 is the only solution, then
t2n − ao + 6h + 6ntn +(B − h − n) � 0. (32)
P � (6A + 1)(6 · 0 + 1) � 6 + 11 � (P − 1 + 1)1,
P− 1
A ≡ 5(r + t) (mod6). (36) Tus, a composite provided the values for t and r are
integers.
Clearly, if the values of t and r are not integers, then P is a
Proof. From prime. □
P � (6t + 5)(6r + 5) � 6[6tr + 5(r + t)] + 25, (37)
2.5. Factorizing the Composite of Case 4. Suppose P � (6t +
then
5)(6r + 5) is a composite. Ten, there exists some positive
P − 25 � 6[6tr + 5(r + t)]. (38) integer A such that A ≡ 5(t + r) (mod6)) as in (38). Let
Corollary 3. If P � (6t + 5)(6r + 5) has at least a nonzero For maximum η, we choose the negative integer solution
integer solution, then P is a composite. of f in (52).
Suppose ho is the negative value of f; then, we obtain
√�� √�� √��
Proof. Suppose there exists a suitable value of 5(r + t) say 5( P − 5)2 − 6 ao + 6ho ( P − 5) + 180 B − ho 5 P
5(r + t) � s ≠ A. Ten, u � tr ≠ 0. η≤ √ �� ≈ . (53)
36 P 36
Let rt � u. Ten, t(s/5 − t) � u:
Ten, we can now obtain the exact integer solution for t
st − 5t2 − 5u � 0, recursively in simply ≤ η + ω iterations. When we obtain
(44)
5t2 − st + 5u � 0. exact integer solutions for t, then it implies that at least one
of the t values gives a prime or a number that divides P.
Journal of Mathematics 5
P � (6 · 0 + 1)(6A + 5) � 1 · 6 + 5,
3. Composites of the Form (6k + 5) and P− 5
Their Factorization 6 (65)
Theorem 4. Let P � (6t + 5)(6r + 1); then, there exist some P � 1 · P.
integer A such that
Implying if (56) has only a trivial solution, then 1 and P
A ≡ (5r + t) (mod6). (54) are the only divisors of P. □
⇒ t − u � 0,
P− 5 s− t (66)
� (5r + t) + 6tr. (57)
6 5
Let A � P − 5/6 and k � tr; then,
t2 − st + 5u � 0.
A � (5r + t) + 6k, (58)
Applying quadratic formula, we obtain
implying �������
s ± s2 − 20u (67)
A ≡ (5r + t) (mod6). (59) t� .
□ 2
√�������
√������
Te �
solution is either (s + s2 − 20u /2) or
Lemma 3. If 5r + t � A, then either r � (A/5), t � 0 or r � (s − s2 − 20u /2).
Substituting the solution in (56), we obtain
������� �������
0, t � A is a trivial solution to (56).
P �⎛⎝6 + 5⎞⎠⎛
⎝6 ⎠ (68)
+ 1⎞
2
s + s − 20u s − s2 − 20u
Proof. Let 2 10
5r + t � A. (60)
or
Ten, ������� �������
P �⎝
⎛6 s − + 5⎞⎠⎛
⎝6 ⎠. (69)
+ 1⎞
s2 − 20u s + s2 − 20u
rt � 0, (61) 2 10
√�������
√������
P is�a composite provided (s + s2 − 20u /2) and
implying that
Tus,
⇒t
□
� 0.
A− t A− t (s − s2 − 20u /2) are integers.
r� (62)
5 5
Tus, t � 0 or t � A, when t � 0, r � (A/5) and when
t � A, r � 0. □ 3.1. Factorizing the Composite P � (6t + 5)(6r + 1).
Suppose P � (6t + 5)(6r + 1) is a composite. Ten, there
exists some positive integer A such that A ≡ 5r + t (mod6)
Corollary 4. When the trivial solution in Lemma 3 is the only
as in (56).
solution to (56), then P is either prime or divisible by 5.
Let
Proof. From (56), we need to show that P is either divisible 5r + t � ao + 6f, (70)
by 5 or has exactly two divisors that is 1 and P itself: such that A − ao is divisible by 6, where f � 0, 1, 2, . . . , n
P � (6r + 1)(6t + 5). (63) and ao is the smallest positive residue. Ten,
P � 6 + 1(6 · 0 + 5) � 6 + 15.
A P− 5 where B � (A − ao /6).
(64)
5 30 From (72) and (73), we obtain
Ten, P is divisible by 5, provided 5 divides A. t2 − ao + 6ft + 5(B − f) � 0. (72)
Or when t � A, r � 0, then
6 Journal of Mathematics
Since we are interested in the positive integer solution, (iv) We obtain the least positive integer fmin � h
then such that
t2 − (ao + 6f)t + (B − f) � 0 has a real
ao + 6f − 20(B − f) ≥ 0.
2
(73) solution.
By expanding, we obtain Tis is obtained from (ao + 6f)2 − 4(B−
f) ≥ 0. √��
36f2 + 12ao + 20f − 20B + a2o ≥ 0. (74) (v) We obtain the integer n from n � η ≤ ( P /36),
where ho � − h.
Ten, let h be the least positive integer solution for f (vi) n � 0, 1, 2, . . . , η, �����������������
fo � h, fn � h + n,� and
satisfying (76). Ten, fn � h + n, where n � 0, 1, . . . , η. Tis
implies that fmin � h for the real solution in (75). to � ((ao + 6h) ± (ao + 6h)2 − 4(B − h) /2).
Substituting fn � h + n in (75), we obtain If the integer solution does not exist at n � 0,
we do step (vii)
t2n − ao + 6h + 6ntn + 5(B − h − n) � 0. (75) (vii) We perform at most η iterations to obtain the
√�� integer solutions for t from
�������������������������
√��
However, we know from [7] that 6t + 5 ≤ P; then,
ao + 6h + 6n ± ao + 6h + 6n − 4(B − h − n)
2
t ≤ ( P − 5/6). Substituting for t in (77) to determine the tn � .
maximum value for n � η, we obtain 2
√�� √�� √�� (79)
( P − 5)2 − 6 ao + 6h( P − 5) + 180(B − h)
�
�
P
n≤ √ ≈ . (76)
36 P 36 Remark: If the values of t seem to converge at
some point on a real line, then it is better to list the
For maximum η, we choose the negative integer solution integer values of t from the point of convergence
of f in (76). (integer value of convergence) to as small as 1;
Suppose ho is the negative value of f; then, we obtain
√�� √�� √��
that is, if the integer value of convergence of t is τ,
( P − 5)2 − 6 ao + 6ho ( P − 5) + 180 B − ho then we list them such that t � τ − 1, . . . , 1 and
√��
P
η≤ ≈ . (77) do the trial divisions from P/6t + 1.
36 P 36
If there is no integer value of t such that
Ten, we can now obtain the exact integer solution for t (P/6t + 1) is an integer, then we try Case 4.
recursively in simply ≤ η iterations. When we obtain exact OR Case 4
integer solutions for t, then it implies that at least one of the t (i) We determine integer A from A � (P − 25/6).
values gives a prime or a number that divides P. (ii) We write down the congruence:
A ≡ 5(r + t) (mod6). (80)
4. General Outline of the Algorithm
(iii) We determine B and ao , where ao is the least
We outline the steps of factorizing primes based on the positive residue such that B � (A − ao /6) is a
proofs above. positive integer.
Given an integer P (iv) We obtain the least positive integer fmin � h
(1) Check if P satisfes P2 ≡ 1 (mod24). such that
(2) Does (1) hold? If so, do step (4), and if not, do step 5t2 − (ao + 6f)t + 5(B − f) � 0 has a real
(3). solution.
Tis is obtained from (ao + 6f)2 − 100(B −
√��
(3) Check if it is either divisible by 2 or by 3 and reduced f) ≥ 0.
it by dividing by 2 or 3 until the Quotient is no longer (v) We obtain the integer n from n � η ≤ (5 P /36),
divisible by 2 or 3. If the resulting Quotient is greater where ho � − h.
than 3, we can call it P and do step (4). (vi) n � 0, 1, 2, . . . �������������������
, η, � to �
fo � h, fn � h + n, and
(4) Check if P is either of the form 6k + 1 or 6k + 5 by
((ao + 6h) ± (ao + 6h)2 − 100(B − h) /10).
subtracting 1 or 5 from P and check if the diference
If the integer solution does not exist at n � 0,
is divisible by 6.
we do step (vii).
(5) If P is of the form 6k + 1, then (vii) We perform at most η iterations to obtain the
���������������������������
Either Case 3 integer solutions for t from
(i) We determine integer A from A � (P − 1/6). ao + 6h + 6n ± ao + 6h + 6n − 100(B − h − n)
2
(iii) We determine B and ao , where ao is the least Remark: If the values of t seem to converge at
positive residue such that B � A − ao /6 is a some point on a real line, then it is better to list
positive integer the integer values of t from the point of
Journal of Mathematics 7
No
No
Is ti=t an integer? Is i=η?
Yes Yes
Yes
Compute P Is ti≤1?
6t+1
No No
Is P an integer?
6t+1
ti=1,...,τ−1,tη=τ
ERROR
Yes t=t+1
Compute P1= P
6t+1
Read P1 and P2 No
Yes No
Is P1 an integer? Is t=τ−1?
STOP
PRIME Yes Yes
convergence (integer value of convergence) to (vii) We perform at most η iterations to obtain the
as small as 1; that is, if the integer value of �������������������������
integer solutions for t from �
convergence of t is τ, then we list them such that ao + 6h + 6n ± ao + 6h + 6n − 20(B − h − n)
2
No
No
Is ti=t an integer? Is i=η?
Yes Yes
Yes
Compute P Is ti≤1?
6t+5
No No
Is P an integer?
6t+5
ti=1,...,τ−1,tη=τ
ERROR
Yes t=t+1
Compute P1= P
6t+1
Read P1 and P2 No
Yes No
Is P1 an integer? Is t=τ−1?
STOP
PRIME Yes
Yes
TRY CASE I Done CASE I?
No
Figure 2: Algorithm for numbers of the form 6k + 1: Case 4.
No
No
Is ti=t an integer? Is i=η?
Yes Yes
Yes
Compute P Is ti≤1?
6t+5
No No
Is P an integer?
6t+5
ti=1,...,τ−1,tη=τ
ERROR
Yes t=t+1
Compute P1*=
P P
Read P1 and P2 6t+5 ,P2* 6t+1 No
Yes No
Is P1* or P2* an integer? Is t=τ−1?
STOP
Yes
PRIME
[21] H.•M. Sun, M.•E. Wu, W.•C. Ting, and M. Hinek, “Dual RSA
and its security analysis,” IEEE Transactions on Information
Teory, vol. 53, no. 8, pp. 2922–2933, 2007.
[22] G. P. Michon, “Factoring into primes,” 2020, http://www.
numericana.com/answer/factoring.htm.
[23] R. L. Rivest, A. Shamir, and L. Adleman, “A method for
obtaining digital signatures and public•key cryptosystems,”
Communications of the ACM, vol. 21, no. 2, pp. 120–126, 1978.
[24] M. Agrawal, N. Kayal, and N. Saxena, “PRIMES is in P,”
Annals of Mathematics, vol. 160, no. 2, pp. 781–793, 2004.
[25] C. Pomerance, “On the distribution of pseudoprimes,”
Mathematics of Computation, vol. 37, no. 156, pp. 587–593,
1981.
[26] R. Hans, Prime Number And Computer Methods for Factor•
ization irkhauser, p. pg3, Birkhäuser, Boston, MA, USA,
2012.
[27] L. M. Adleman, C. Pomerance, and R. S. Rumely, “On dis•
tinguishing prime numbers from composite numbers,” An•
nals of Mathematics, 1983.
[28] R. Schoof, “Four primality testing algorithms,” Algorithmic
Number Teory, vol. 44, 2008.
[29] H. W. Lenstra, “Factoring integers with elliptic curves,”
Annals of Mathematics, vol. 126, no. 3, pp. 649–673, 1987.
[30] J. M. Pollard, “Teorems on factorization and primality
testing,” Mathematical Proceedings of the Cambridge Philo•
sophical Society, vol. 76, no. 3, pp. 521–528, 1974.
[31] C. Harvey, Advanced Number Teory, p. pg10, Dover Pub•
lications. Inc, New York, NY, USA, 1980.
[32] G. Tenenbaum and M. Mendes France, Te Prime Numbers
and Teir Distribution, American Mathematical Society,
Washington, WA, USA, 1997.
[33] R. D. Carmichael, “On composite numbers p which satisfy the
Fermat congruence.ap•1�1modp,” Te American Mathemat•
ical Monthly, vol. 19, pp. 22–27, 1912.
[34] M. L. Weber, “It’s a wonderful world • and universe • out
there,” 2020, https://www.snexplores.org/article/explainer•
what•is•an•algorithm.
[35] J. W. Porras Ferreira and C. G. Willian De Jesus, “An Analysis
of the Congruence 1mod24 as a Generator of prime numbers
Greater or Equal to 5,” Current Journal of Applied Science and
Technology, vol. 14, pp. 2231–0843, 2016, http://
sciencedomain.org/review•history/13307.
[36] Z. Joseph and B. Joseph Hajj, “Prime factorization using
square root approximation,” Computers & Mathematics with
Applications, vol. 61, 2011, http://www.sciencedirect.com/
science/article/pii/S0898122111001131.