One of the frequent headache in the world of mathematics is to determine whether a number is prime or not. This time will be given examples of programs to find or check whether a number is included in the primes.
As is well known, prime number is a number that is only divisible by 1 and the number itself. Numbers 1 because it is a divider factor can not be called a prime number. Then as a starting point the calculation, the smallest prime number that must be known. In this case will be taken number 2 and 3 as the smallest prime number. Number 2 is a prime number which is unique because it is the only even prime. The other primes must be odd (odd).
As is well known, prime number is a number that is only divisible by 1 and the number itself. Numbers 1 because it is a divider factor can not be called a prime number. Then as a starting point the calculation, the smallest prime number that must be known. In this case will be taken number 2 and 3 as the smallest prime number. Number 2 is a prime number which is unique because it is the only even prime. The other primes must be odd (odd).
Well, simply, a program to find prime numbers are given on the listing..
Listing 1. Finding primes:
#include <stdio.h>main(){int bilangan;int prima;int cekprima();clrscr();printf("======================\n");printf("FIND PRIMA NUMBERS\n");printf("======================\n\n");printf("Enter an integer : ");scanf("%d",&number);prima = cekprima(number);if (prima == 1){printf("number %d is prime",number);} else {printf("number %d not a prime number",number);}}int cekprima(bil)int bil;{int toward=3;int boundary;if (bil == 1){return(0);} else if (bil==2||bil==3) {return(1);} else if (bil % 2 == 0) {return(0);} else {while (boundary > toward){if (bil % toward == 0){printf("Because the divisible %d\n",toward);return(0);break;}boundary = bil / toward;toward += 2;}return(1);}}Listing 2. Finding primes in the range 1-1000#include <stdio.h>main(){int i;int prima;int cekprima();clrscr();printf("======================\n");printf("FIND PRIMA NUMBERS\n");printf("in the range 1-1000\n");printf("======================\n\n");printf("Primes found : \n");for (i=1;i<=1000;i++){prima = cekprima(i);if (prima == 1){printf(" %d",i);}}}int cekprima(bil)int bil;{int bagi=3;int batas;if (bil == 1){return(0);} else if (bil==2||bil==3) {return(1);} else if (bil % 2 == 0) {return(0);} else {while (batas > bagi){if (bil % bagi == 0){return(0);break;}batas = bil / bagi;bagi += 2;}return(1);}}
0 komentar:
Post a Comment