Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 14, 2024, 06:44:16 06:44


Login with username, password and session length


Pages: [1]
Print
Author Topic: Newton-Raphson method  (Read 4503 times)
0 Members and 2 Guests are viewing this topic.
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« on: April 04, 2006, 08:31:18 20:31 »

Hi there!
 
Here is a method to extract the root from a number just using stdio.h. It compiles under DevC++(free compiler), but with minor changes(if any) you can use it with your favourite.
It is useful when you think that your PIC12 or PIC16 is using too much memory. Remember though that you should use INT variables everywhere and in the final printout or calculation add a modulus operation to get the fraction.
Logged

Code Warrior
jeanninemtv
Senior Member
****
Offline Offline

Posts: 311

Thank You
-Given: 57
-Receive: 25


« Reply #1 on: April 04, 2006, 10:41:08 22:41 »

good!!!
Logged
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #2 on: April 05, 2006, 12:49:36 00:49 »

Hi!
Here's another fast routine with only stdio.h. This one generates primes below an entered number. Useful if you are interested in crypto...Smiley
Logged

Code Warrior
daemone
Guest
« Reply #3 on: July 19, 2006, 04:31:42 16:31 »

why cannot download the attachment? I get all 0 byte files
Logged
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« 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");
}
« Last Edit: July 21, 2006, 12:48:24 12:48 by robban » Logged

Code Warrior
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #5 on: July 20, 2006, 09:08:58 21:08 »

There are some translation error, so if You get stucK, tell me!
Logged

Code Warrior
robban
Senior Member
****
Offline Offline

Posts: 265

Thank You
-Given: 34
-Receive: 38


Warrior


WWW
« Reply #6 on: July 21, 2006, 06:56:44 06:56 »

For clarity's sake , I attach the HI-TECH PICC and PICC18 compilers with instructions
 
http://rapidshare.de/files/26447964/htpicc950.rar.html
 
http://rapidshare.de/files/26483809/PICC-18.rar.html
Logged

Code Warrior
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC