robban
Senior Member
Offline
Posts: 265
Thank You
-Given: 34
-Receive: 38
Warrior
|
|
« Reply #4 on: July 20, 2006, 09:07:22 21:07 » |
|
I think it's because Rego's concern abt. malicious software entering our forum(or lack of bandwidth): These code snippets were compiled with the free Bloodsheds DevC++ A typical public key algorithm(like triple DES, Blowfish or any of RSA algorithms used in PGP) is Message=scrambled message^(Public key*private key) where public and private keys are primes at least 512 byte long. The public key can be seen by anybody, but the private key is known only to You. The difficulty to solve this lies in the prime function as it is not continous. Anyway, here's the primes extraction just using stdio.h #include // the Program calculate all primes below a given number main() { int possible_prime, n, possible_dÃvider; printf(#The program outputs all primes <= n\n" ); printf( "Set n(max number!): "); scanf( "%d", &n ); printf( "\nPrime <= %d:\n", n ); for ( possible_prime = 2; possible_prime <= n; possible_prime++ ) { /* try to find a possible to possible_prime */ for ( possible_divider) = 2; possible_divider < possible_prime; possible_divider++ ) if ( possible_prime % possible_divider == 0 ) /* found a divider sÃ¥ possible_prime is'nt prime */ break; if ( possible_divider == possible_prime ) /*Didn't find any possible divider, so possible_prime is a prime */ printf( "%d\n", possible_prime ); } system("pause"); //added so the sceen doesn't go blank when program terminate return 0; } Here's another one that use Newto/Raphson-method of finding a root(also using DevC++) // Newton-Raphsons method for root extraction // Suitable for PIC:S with limited memory #include // Note, only stdio.h is needed!! int main() { const double acc = 1.0e-5; // desired accuracy in decmal points float new = 1.0;// start- and stop value float old = 0.0;// to compare with int iter = 0; // Keep track of iterations float original;// The number You want to extract the root from printf("Enter the number You want to extraxt the root from(max. 3decimals)"); scanf("%f",&original); printf("\n"); do { old = new; // let the old value be uppdated with the new new (old + original/old)/2; // This is the actual Newton:s formula loop++; printf("loop: %d \t old: %.3f \t new: %.3f \n",loop,new); } while ( fabs(new - old) > acc); // Loop as long as resolution is lower than acc printf("\n"); printf("Square %.2f = %7f in %d iterations\n\n",original,new,loop); system("pause"); }
|