Prime Factor - ในทฤษฎีจำนวน ตัวประกอบเฉพาะของจำนวนเต็มบวก คือ เลขเฉพาะที่หารจำนวนนั้นลงตัว กระบวนการหาเรียกว่า Prime Factorization
ตัวอย่าง: Prime factors ของ 288 = 2 x 2 x 2 x 2 x 2 x 3 x 3
Input: n = 124 Output: 31 is the largest prime factor!
คำอธิบาย
หาตัวประกอบเฉพาะทั้งหมด แล้วเอาตัวมากที่สุด 124 = 2 x 2 x 31 -> มากที่สุดคือ 31
ตัวอย่าง
#include <stdio.h>
int main() {
long int n = 3453;
long int div = 2, ans = 0, maxFact;
while (n != 0) {
if (n % div != 0)
div = div + 1;
else {
maxFact = n;
n = n / div;
if (n == 1) {
printf("%d is the largest prime factor !", maxFact);
ans = 1;
break;
}
}
}
return 0;
}ผลลัพธ์
1151 is the largest prime factor !