www.bilisim-egitim.tr.gg |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Bir sayıyı asal çarpanlarına ayıran program
#include <stdio.h> /* printf,scanf */
#include <stdlib.h> /* EXIT_SUCCESS */
#include <math.h> /* sqrt */
typedef enum { FALSE, TRUE } bool;
int next_prime(int prime);
int main(void)
{
int number, factor;
printf("Sayı: "); scanf("%d", &number);
factor = 2;
while (number > 1)
{
while (number % factor == 0)
{
printf("%d ", factor);
number /= factor;
}
factor = next_prime(factor);
}
printf("n");
return EXIT_SUCCESS;
}
bool is_prime(int cand)
{
int count;
if (cand == 2)
return TRUE;
if (cand % 2 == 0)
return FALSE;
for (count = 3; count <= sqrt(cand); count += 2)
{
if (cand % count == 0)
return FALSE;
}
return TRUE;
}
int next_prime(int prime)
{
int cand = (prime % 2 == 0) ? prime + 1 : prime + 2;
while (!is_prime(cand))
cand += 2;
return 0;
/*www.bilisim-egitim.tr.gg*/
}
|
|
|
|
|
|
|
Bugün 163 ziyaretçi (252 klik)
www.bilisim-egitim.tr.gg
|
|
|
|
|
|
|
|