| | |
Factorizer
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
This program prints the prime factors of a number. Eg. 56=2x2x2x7 .I've heard its really important and stuff.I believe it is really efficient and simple. Uses two functions : one to check if a number is prime and other to return the next prime number.
#include <stdio.h> #include <conio.h> int IsPrime( int ); int FindNextPrime( int ); int main() { int num,divisor = 2; printf("Enter number:"); scanf("%d",&num ); printf("The prime factors are:\n"); while( !IsPrime(num)) { while(1) { if( num%divisor == 0 ) { printf("%d\n",divisor ); num/=divisor; break; } divisor = FindNextPrime( divisor ); } } printf("%d",num); getch(); return 0; } //------ End of main() ------ int IsPrime( int num ) // Checks whether a number is prime { int i; for( i=2; i<=num/2; i++ ) { if( num%i == 0 ) return 0; } return 1; } int FindNextPrime( int num ) // Returns next prime number from passed arg. { do { if( num == 2 ) num++; else num+=2; } while( !IsPrime(num)); return num; }
0
•
•
•
•
errrm... i just have a question: is it possible not to use isprime to search for the prime factors of a number?
0
•
•
•
•
To nlsna17: prime figures only get null rest (same as integer result) when divided by themselves and by 1. Let it be N, start dividing N/(N-1), N/(N-2), .. until N/2. If ALL these divisions do not render null rest, it is ALL rests > 0, it is ALL Real results, not a single integer result, then N (Integer as well indeed) is prime.
Sure there are faster ways to find out because I am aware of pundits using large primes to hide/cypher data, but I hope the above definition may help as a starting point.
Bofarull
Sure there are faster ways to find out because I am aware of pundits using large primes to hide/cypher data, but I hope the above definition may help as a starting point.
Bofarull
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h



