# Prime Factors

• Script Name Prime Factors
• Description Prime factors calculation
• Category SQL General
• Contributor jackattack
• Created Saturday June 10, 2017
• Statement 1

Prime factors of a Natural number (ie. 21)

``````WITH naturals AS (
SELECT  level+1 i
FROM    dual
CONNECT BY LEVEL <= 999
)
, primes AS (
SELECT  n.i
FROM    naturals n
WHERE   NOT EXISTS (
SELECT 1
FROM   naturals n2
WHERE  n2.i <= SQRT(n.i)
AND REMAINDER(n.i, n2.i) = 0
)
)
SELECT  p.i
FROM    primes p
WHERE   REMAINDER(21, p.i) = 0 ``````
I
3
7

2 rows selected.
• Statement 2

Natural numbers < 1000 with at least 4 prime factors

``````WITH
naturals AS (
SELECT  level+1 i
FROM    dual
CONNECT BY LEVEL <= 999
),
primes AS (
SELECT  n.i
FROM    naturals n
WHERE   NOT EXISTS (
SELECT 1
FROM   naturals n2
WHERE  n2.i <= SQRT(n.i)
AND REMAINDER(n.i, n2.i) = 0
)
)
SELECT  n.i n, LISTAGG(p.i, ', ') WITHIN GROUP (ORDER BY p.i) prime_factors
FROM    naturals n CROSS JOIN primes p
WHERE   REMAINDER(n.i, p.i) = 0
group   by n.i
HAVING  COUNT(*) > 3
order   by 1 ``````
NPRIME_FACTORS
2102, 3, 5, 7
3302, 3, 5, 11
3902, 3, 5, 13
4202, 3, 5, 7
4622, 3, 7, 11
5102, 3, 5, 17
5462, 3, 7, 13
5702, 3, 5, 19
6302, 3, 5, 7
6602, 3, 5, 11
6902, 3, 5, 23
7142, 3, 7, 17
7702, 5, 7, 11
7802, 3, 5, 13
7982, 3, 7, 19
8402, 3, 5, 7
8582, 3, 11, 13
8702, 3, 5, 29
9102, 5, 7, 13
9242, 3, 7, 11
9302, 3, 5, 31
9662, 3, 7, 23
9902, 3, 5, 11

23 rows selected.
• Statement 3

Natural numbers < 1000, prime factors

``````WITH
naturals AS (
SELECT  level+1 i
FROM    dual
CONNECT BY LEVEL <= 999
),
primes AS (
SELECT  n.i
FROM    naturals n
WHERE   NOT EXISTS (
SELECT 1
FROM   naturals n2
WHERE  n2.i <= SQRT(n.i)
AND REMAINDER(n.i, n2.i) = 0
)
)
SELECT  n.i n, LISTAGG(p.i, ', ') WITHIN GROUP (ORDER BY p.i) prime_factors
FROM    naturals n CROSS JOIN primes p
WHERE   REMAINDER(n.i, p.i) = 0
GROUP   BY n.i
ORDER   BY 1 ``````
NPRIME_FACTORS
22
33
42
55
62, 3
77
82
93
102, 5
1111
122, 3
1313
142, 7
153, 5
162
1717
182, 3
1919
202, 5
213, 7
222, 11
2323
242, 3
255
262, 13
273
282, 7
2929
302, 3, 5
3131
322
333, 11
342, 17
355, 7
362, 3
3737
382, 19
393, 13
402, 5
4141
422, 3, 7
4343
442, 11
453, 5
462, 23
4747
482, 3
497
502, 5
513, 17
522, 13
5353
542, 3
555, 11
562, 7
573, 19
582, 29
5959
602, 3, 5
6161
622, 31
633, 7
642
655, 13
662, 3, 11
6767
682, 17
693, 23
702, 5, 7
7171
722, 3
7373
742, 37
753, 5
762, 19
777, 11
782, 3, 13
7979
802, 5
813
822, 41
8383
842, 3, 7
855, 17
862, 43
873, 29
882, 11
8989
902, 3, 5
917, 13
922, 23
933, 31
942, 47
955, 19
962, 3
9797
982, 7
993, 11
1002, 5
101101
1022, 3, 17
103103
1042, 13
1053, 5, 7
1062, 53
107107
1082, 3
109109
1102, 5, 11
1113, 37
1122, 7
113113
1142, 3, 19
1155, 23
1162, 29
1173, 13
1182, 59
1197, 17
1202, 3, 5
12111
1222, 61
1233, 41
1242, 31
1255
1262, 3, 7
127127
1282
1293, 43
1302, 5, 13
131131
1322, 3, 11
1337, 19
1342, 67
1353, 5
1362, 17
137137
1382, 3, 23
139139
1402, 5, 7
1413, 47
1422, 71
14311, 13
1442, 3
1455, 29
1462, 73
1473, 7
1482, 37
149149
1502, 3, 5
151151
1522, 19
1533, 17
1542, 7, 11
1555, 31
1562, 3, 13
157157
1582, 79
1593, 53
1602, 5
1617, 23
1622, 3
163163
1642, 41
1653, 5, 11
1662, 83
167167
1682, 3, 7
16913
1702, 5, 17
1713, 19
1722, 43
173173
1742, 3, 29
1755, 7
1762, 11
1773, 59
1782, 89
179179
1802, 3, 5
181181
1822, 7, 13
1833, 61
1842, 23
1855, 37
1862, 3, 31
18711, 17
1882, 47
1893, 7
1902, 5, 19
191191
1922, 3
193193
1942, 97
1953, 5, 13
1962, 7
197197
1982, 3, 11
199199
2002, 5
2013, 67
2022, 101
2037, 29
2042, 3, 17
2055, 41
2062, 103
2073, 23
2082, 13
20911, 19
2102, 3, 5, 7
211211
2122, 53
2133, 71
2142, 107
2155, 43
2162, 3
2177, 31
2182, 109
2193, 73
2202, 5, 11
22113, 17
2222, 3, 37
223223
2242, 7
2253, 5
2262, 113
227227
2282, 3, 19
229229
2302, 5, 23
2313, 7, 11
2322, 29
233233
2342, 3, 13
2355, 47
2362, 59
2373, 79
2382, 7, 17
239239
2402, 3, 5
241241
2422, 11
2433
2442, 61
2455, 7
2462, 3, 41
24713, 19
2482, 31
2493, 83
2502, 5
251251
2522, 3, 7
25311, 23
2542, 127
2553, 5, 17
2562
257257
2582, 3, 43
2597, 37
2602, 5, 13
2613, 29
2622, 131
263263
2642, 3, 11
2655, 53
2662, 7, 19
2673, 89
2682, 67
269269
2702, 3, 5
271271
2722, 17
2733, 7, 13
2742, 137
2755, 11
2762, 3, 23
277277
2782, 139
2793, 31
2802, 5, 7
281281
2822, 3, 47
283283
2842, 71
2853, 5, 19
2862, 11, 13
2877, 41
2882, 3
28917
2902, 5, 29
2913, 97
2922, 73
293293
2942, 3, 7
2955, 59
2962, 37
2973, 11
2982, 149
29913, 23
3002, 3, 5
3017, 43
3022, 151
3033, 101
3042, 19
3055, 61
3062, 3, 17
307307
3082, 7, 11
3093, 103
3102, 5, 31
311311
3122, 3, 13
313313
3142, 157
3153, 5, 7
3162, 79
317317
3182, 3, 53
31911, 29
3202, 5
3213, 107
3222, 7, 23
32317, 19
3242, 3
3255, 13
3262, 163
3273, 109
3282, 41
3297, 47
3302, 3, 5, 11
331331
3322, 83
3333, 37
3342, 167
3355, 67
3362, 3, 7
337337
3382, 13
3393, 113
3402, 5, 17
34111, 31
3422, 3, 19
3437
3442, 43
3453, 5, 23
3462, 173
347347
3482, 3, 29
349349
3502, 5, 7
3513, 13
3522, 11
353353
3542, 3, 59
3555, 71
3562, 89
3573, 7, 17
3582, 179
359359
3602, 3, 5
36119
3622, 181
3633, 11
3642, 7, 13
3655, 73
3662, 3, 61
367367
3682, 23
3693, 41
3702, 5, 37
3717, 53
3722, 3, 31
373373
3742, 11, 17
3753, 5
3762, 47
37713, 29
3782, 3, 7
379379
3802, 5, 19
3813, 127
3822, 191
383383
3842, 3
3855, 7, 11
3862, 193
3873, 43
3882, 97
389389
3902, 3, 5, 13
39117, 23
3922, 7
3933, 131
3942, 197
3955, 79
3962, 3, 11
397397
3982, 199
3993, 7, 19
4002, 5
401401
4022, 3, 67
40313, 31
4042, 101
4053, 5
4062, 7, 29
40711, 37
4082, 3, 17
409409
4102, 5, 41
4113, 137
4122, 103
4137, 59
4142, 3, 23
4155, 83
4162, 13
4173, 139
4182, 11, 19
419419
4202, 3, 5, 7
421421
4222, 211
4233, 47
4242, 53
4255, 17
4262, 3, 71
4277, 61
4282, 107
4293, 11, 13
4302, 5, 43
431431
4322, 3
433433
4342, 7, 31
4353, 5, 29
4362, 109
43719, 23
4382, 3, 73
439439
4402, 5, 11
4413, 7
4422, 13, 17
443443
4442, 3, 37
4455, 89
4462, 223
4473, 149
4482, 7
449449
4502, 3, 5
45111, 41
4522, 113
4533, 151
4542, 227
4555, 7, 13
4562, 3, 19
457457
4582, 229
4593, 17
4602, 5, 23
461461
4622, 3, 7, 11
463463
4642, 29
4653, 5, 31
4662, 233
467467
4682, 3, 13
4697, 67
4702, 5, 47
4713, 157
4722, 59
47311, 43
4742, 3, 79
4755, 19
4762, 7, 17
4773, 53
4782, 239
479479
4802, 3, 5
48113, 37
4822, 241
4833, 7, 23
4842, 11
4855, 97
4862, 3
487487
4882, 61
4893, 163
4902, 5, 7
491491
4922, 3, 41
49317, 29
4942, 13, 19
4953, 5, 11
4962, 31
4977, 71
4982, 3, 83
499499
5002, 5
5013, 167
5022, 251
503503
5042, 3, 7
5055, 101
5062, 11, 23
5073, 13
5082, 127
509509
5102, 3, 5, 17
5117, 73
5122
5133, 19
5142, 257
5155, 103
5162, 3, 43
51711, 47
5182, 7, 37
5193, 173
5202, 5, 13
521521
5222, 3, 29
523523
5242, 131
5253, 5, 7
5262, 263
52717, 31
5282, 3, 11
52923
5302, 5, 53
5313, 59
5322, 7, 19
53313, 41
5342, 3, 89
5355, 107
5362, 67
5373, 179
5382, 269
5397, 11
5402, 3, 5
541541
5422, 271
5433, 181
5442, 17
5455, 109
5462, 3, 7, 13
547547
5482, 137
5493, 61
5502, 5, 11
55119, 29
5522, 3, 23
5537, 79
5542, 277
5553, 5, 37
5562, 139
557557
5582, 3, 31
55913, 43
5602, 5, 7
5613, 11, 17
5622, 281
563563
5642, 3, 47
5655, 113
5662, 283
5673, 7
5682, 71
569569
5702, 3, 5, 19
571571
5722, 11, 13
5733, 191
5742, 7, 41
5755, 23
5762, 3
577577
5782, 17
5793, 193
5802, 5, 29
5817, 83
5822, 3, 97
58311, 53
5842, 73
5853, 5, 13
5862, 293
587587
5882, 3, 7
58919, 31
5902, 5, 59
5913, 197
5922, 37
593593
5942, 3, 11
5955, 7, 17
5962, 149
5973, 199
5982, 13, 23
599599
6002, 3, 5
601601
6022, 7, 43
6033, 67
6042, 151
6055, 11
6062, 3, 101
607607
6082, 19
6093, 7, 29
6102, 5, 61
61113, 47
6122, 3, 17
613613
6142, 307
6153, 5, 41
6162, 7, 11
617617
6182, 3, 103
619619
6202, 5, 31
6213, 23
6222, 311
6237, 89
6242, 3, 13
6255
6262, 313
6273, 11, 19
6282, 157
62917, 37
6302, 3, 5, 7
631631
6322, 79
6333, 211
6342, 317
6355, 127
6362, 3, 53
6377, 13
6382, 11, 29
6393, 71
6402, 5
641641
6422, 3, 107
643643
6442, 7, 23
6453, 5, 43
6462, 17, 19
647647
6482, 3
64911, 59
6502, 5, 13
6513, 7, 31
6522, 163
653653
6542, 3, 109
6555, 131
6562, 41
6573, 73
6582, 7, 47
659659
6602, 3, 5, 11
661661
6622, 331
6633, 13, 17
6642, 83
6655, 7, 19
6662, 3, 37
66723, 29
6682, 167
6693, 223
6702, 5, 67
67111, 61
6722, 3, 7
673673
6742, 337
6753, 5
6762, 13
677677
6782, 3, 113
6797, 97
6802, 5, 17
6813, 227
6822, 11, 31
683683
6842, 3, 19
6855, 137
6862, 7
6873, 229
6882, 43
68913, 53
6902, 3, 5, 23
691691
6922, 173
6933, 7, 11
6942, 347
6955, 139
6962, 3, 29
69717, 41
6982, 349
6993, 233
7002, 5, 7
701701
7022, 3, 13
70319, 37
7042, 11
7053, 5, 47
7062, 353
7077, 101
7082, 3, 59
709709
7102, 5, 71
7113, 79
7122, 89
71323, 31
7142, 3, 7, 17
7155, 11, 13
7162, 179
7173, 239
7182, 359
719719
7202, 3, 5
7217, 103
7222, 19
7233, 241
7242, 181
7255, 29
7262, 3, 11
727727
7282, 7, 13
7293
7302, 5, 73
73117, 43
7322, 3, 61
733733
7342, 367
7353, 5, 7
7362, 23
73711, 67
7382, 3, 41
739739
7402, 5, 37
7413, 13, 19
7422, 7, 53
743743
7442, 3, 31
7455, 149
7462, 373
7473, 83
7482, 11, 17
7497, 107
7502, 3, 5
751751
7522, 47
7533, 251
7542, 13, 29
7555, 151
7562, 3, 7
757757
7582, 379
7593, 11, 23
7602, 5, 19
761761
7622, 3, 127
7637, 109
7642, 191
7653, 5, 17
7662, 383
76713, 59
7682, 3
769769
7702, 5, 7, 11
7713, 257
7722, 193
773773
7742, 3, 43
7755, 31
7762, 97
7773, 7, 37
7782, 389
77919, 41
7802, 3, 5, 13
78111, 71
7822, 17, 23
7833, 29
7842, 7
7855, 157
7862, 3, 131
787787
7882, 197
7893, 263
7902, 5, 79
7917, 113
7922, 3, 11
79313, 61
7942, 397
7953, 5, 53
7962, 199
797797
7982, 3, 7, 19
79917, 47
8002, 5
8013, 89
8022, 401
80311, 73
8042, 3, 67
8055, 7, 23
8062, 13, 31
8073, 269
8082, 101
809809
8102, 3, 5
811811
8122, 7, 29
8133, 271
8142, 11, 37
8155, 163
8162, 3, 17
81719, 43
8182, 409
8193, 7, 13
8202, 5, 41
821821
8222, 3, 137
823823
8242, 103
8253, 5, 11
8262, 7, 59
827827
8282, 3, 23
829829
8302, 5, 83
8313, 277
8322, 13
8337, 17
8342, 3, 139
8355, 167
8362, 11, 19
8373, 31
8382, 419
839839
8402, 3, 5, 7
84129
8422, 421
8433, 281
8442, 211
8455, 13
8462, 3, 47
8477, 11
8482, 53
8493, 283
8502, 5, 17
85123, 37
8522, 3, 71
853853
8542, 7, 61
8553, 5, 19
8562, 107
857857
8582, 3, 11, 13
859859
8602, 5, 43
8613, 7, 41
8622, 431
863863
8642, 3
8655, 173
8662, 433
8673, 17
8682, 7, 31
86911, 79
8702, 3, 5, 29
87113, 67
8722, 109
8733, 97
8742, 19, 23
8755, 7
8762, 3, 73
877877
8782, 439
8793, 293
8802, 5, 11
881881
8822, 3, 7
883883
8842, 13, 17
8853, 5, 59
8862, 443
887887
8882, 3, 37
8897, 127
8902, 5, 89
8913, 11
8922, 223
89319, 47
8942, 3, 149
8955, 179
8962, 7
8973, 13, 23
8982, 449
89929, 31
9002, 3, 5
90117, 53
9022, 11, 41
9033, 7, 43
9042, 113
9055, 181
9062, 3, 151
907907
9082, 227
9093, 101
9102, 5, 7, 13
911911
9122, 3, 19
91311, 83
9142, 457
9153, 5, 61
9162, 229
9177, 131
9182, 3, 17
919919
9202, 5, 23
9213, 307
9222, 461
92313, 71
9242, 3, 7, 11
9255, 37
9262, 463
9273, 103
9282, 29
929929
9302, 3, 5, 31
9317, 19
9322, 233
9333, 311
9342, 467
9355, 11, 17
9362, 3, 13
937937
9382, 7, 67
9393, 313
9402, 5, 47
941941
9422, 3, 157
94323, 41
9442, 59
9453, 5, 7
9462, 11, 43
947947
9482, 3, 79
94913, 73
9502, 5, 19
9513, 317
9522, 7, 17
953953
9542, 3, 53
9555, 191
9562, 239
9573, 11, 29
9582, 479
9597, 137
9602, 3, 5
96131
9622, 13, 37
9633, 107
9642, 241
9655, 193
9662, 3, 7, 23
967967
9682, 11
9693, 17, 19
9702, 5, 97
971971
9722, 3
9737, 139
9742, 487
9753, 5, 13
9762, 61
977977
9782, 3, 163
97911, 89
9802, 5, 7
9813, 109
9822, 491
983983
9842, 3, 41
9855, 197
9862, 17, 29
9873, 7, 47
9882, 13, 19
98923, 43
9902, 3, 5, 11
991991
9922, 31
9933, 331
9942, 7, 71
9955, 199
9962, 3, 83
997997
9982, 499
9993, 37
10002, 5

999 rows selected.