Download UNIT -II

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Law of large numbers wikipedia , lookup

Approximations of π wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Algorithm wikipedia , lookup

Collatz conjecture wikipedia , lookup

Arithmetic wikipedia , lookup

Expected value wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Location arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
1
UNIT – II
FACTORING METHODS
ALGORITHM: #1
FINDING THE SQAUARE ROOT OF A NUMBER
Algorithm description:
1) Read the value of n
2) Set the initial guess r2 as n/2
3) Repeatedly
a) Find the r1 value (estimated value using averaging formula)
b) Let r1 assume the role of r2 until absolute difference between r1 and r2 are less than 0.0001
4) Print the result r2
Algorithm:
Procedure square root(n:integer; absolute: real);
var
r1,
{ previous estimate value of square root}
r2 : real;
{ current estimate value of square root}
{ n is square value , input value}
{ absolute is difference between previous and current square root value}
begin
r2:=n div 2;
repeat
{ finding the previous estimate value of square root using averaging formula}
r1:=(r2 + n /r2) div 2;
absolute=abs(r2-r1);
{ find the current estimate value }
{ find the difference between previous and current estimated value}
r2:=r1;
until absolute>0.0001
writeln(r1);
end
{ repeat the process until greater than 0.0001}
{ display the square root value }
2
ALGORITHM: #2
SMALLEST DEVISOR OF AN INTEGER
Algorithm description:
1) Read the value of n
2) If n is not odd than return 2 as the smallest divisor
else
a) Compute r the square root of n
b) Initialize divisor d to 3
c) Using while check d < r
Substitute in n mod d # 0
then next number is odd sequence d = d + 2
d) If current odd value d is an exact divisor then return exact divisor of n
else return 1 as the smallest divisor of n
Algorithm:
Procedure smallest divisor (n : integer): integer;
var d,
{ divisor value }
r,
{ to find the square root of input value n }
sdivisor : integer;
{ smallest divisor }
begin
r:=sqrt(n);
d:=3;
{ square root of input value n}
{ initialize the divisor value as 3}
while n mod d # 0 &d<r
{ odd number & number less than square root value }
do
d=d+2;
if n mod d=0 then
sdivisor:=d;
else
sdivisor:=1;
end
{ odd value d is an exact divisor then return exact divisor of n}
{ return 1 as the smallest divisor of n }
3
ALGORITHM: #3
GREATEST COMMON DIVISOR OF TWO INTEGERS
Algorithm description:
1) Read the two positive integer (non-zero) x & y
2) Check the given number greater than zero using while loop
3) Check the x value less than y then swap x & y value & find the x value as x – y
4) Print the y value
Algorithm:
Procedure gcd (x, y : integer);
var x,
y,
t : integer;
{ First positive value }
{ second positive value}
{ temporary value}
begin
writeln('enter the two number x and y');
{ Get the input }
readln(x,y);
while x > 0 do
{ Check the given number greater than zero }
begin
if x < y do
begin
t:=x;
x:=y;
y:=t;
end if
x:=x-y;
writeln(x);
end
{ x value less than y then swap x & y value and find the x value}
{ display the value of x}
4
ALGORITHM: #4
GENERATING PRIME NUMBERS
Algorithm description:
1) Read the value of n
2) Initialize ct to 0
3) Check the loop index i values with all j for generate prime numbers
a) Condition of i as 2 to n
b) Condition of j as 2 to j < i
If j < i then i mod j = 0 not considered for generating prime number
4) Print the prime number value
Algorithm:
procedure generate prime ( n : integer);
var
i,
{ loop index value for prime generation }
j,
{ loop index value for check prime factor}
ct : integer;
{ used to check for consideration of prime number if not incremented }
begin
writeln('enter n value');
readln(n);
{ n is limit value for generate prime}
for i=2 to n do
begin
ct==0;
for j=2 to j<i do
begin
if i mod j==0 then
{ if i mod j is 0, then do count index }
ct:=ct+1;
{ count index, not considered for prime number }
if ct = =0 then
writeln(i);
end
end
{ display the generated prime number }
5
ALGORITHM: #5
COMPUTING THE PRIME FACTORS OF AN INTEGER
Algorithm description:
1) Establish n the number whose prime factor to find.
2) Compute the remainder r and quotient q for the first factor
a ) initialize prime factor to generate nextprime as 2 and i a zero
3) While a) nextprime is an exact divisor of n then
i)
Save nextprime as a factor of 5
ii)
Reduce n by nextprime
else
get the next biggest prime from sieve of Eratosthenes
b ) compute next quotient q and remainder r for current value of n and current prime divisor
nextprime
4) If n is greater than 1 then add n to list as a prime factor f.
5) Return the prime factors f of the original number n.
Algorithm
procedure prime factor( var f: nelements, var n: integer);
var q,
{quotient of number n}
r,
{remainder of number n}
nextprime : integer;
{ prime divisor }
d:array[1-100] of integer;
{ multipler array of size}
begin
nextprime := 2;
q := n div nextprime;
{computing quotient}
r := n mod nextprime;
i:=0;
{computing remainder}
while (r = 0) or ( q > nextprime) do
{ quotient greater than nextprime value then
computation proceeds}
begin
if r := 0 then
begin
i:=i+1;
f[i]:=nextprime;
{ exact divisor so save prime factor and reduce n}
6
n:=q;
end
else Eratosthenes (d, nextprime)
{ get next prime}
q := n div nextprime;
{computing quotient}
r := n mod nextprime;
{computing remainder}
end;
if n > 1 then
begin
i : = i + 1;
f[i] : = n
end
end
{ f[1..i] contain all prime factors }
7
ALGORITHM: #6
GENERATION OF PSEUDO-RANDOM NUMBERS
Algorithm description:
1) Selection of x0 , chosen from range 0 < xo< m
2) Selection of m, which is greater than or equal to length of the random sequence required
3) Selection of a depends on choice of m. If m is a power of 2 then satisfy a mod 8 = 5 (or) if m is power
of 10 then satisfy a mod 200 = 21
4) Selection of b which not a multiple of 5
5) Compute x for random number generation
Algorithm
procedure random generation ( a, b, m : integer);
var x,
{ random value generated in output}
a,
{ depends on m value basis on power }
b,
{ odd number not multiple of 5 }
m:integer;
{ required input limit}
begin
x:=0;
m:=4096;
b:=853;
a:=109;
do
begin
x:=(a*x*b)mod m;
{ computation for random number generation}
writeln(x);
{ random value displayed}
end while 0 < = x < m – 1
end
8
ALGORITHM: #7
RAISING A NUMBER TO A LARGE POWER
Algorithm description:
1) Establish n, the integer power and x the integer to be raised to the power n.
2) Initialize the power sequence as x and product as 1.
3) While power n is greater than zero do
a) Find the product value of i th power sequence
b) Find product sequence
i ) multiply accumulate product by current power sequence value
c ) reduce power n by a factor of two using integer division.
d) get next power sequence member by multiplying current value by itself.
4) Return product x raised to the power n.
Algorithm description:
procedure power (var x,n : integer);
var x,
n,
product,
sequence : integer;
{ number to be used for raising power}
{ power of number}
{ current accumulated product}
{ current power sequence}
begin
writeln('enter the power');
readln(x,n);
{ input for compution of raising power}
product:=1;
sequence:=x;
{ initialization of product as 1 and sequence as x}
while n > 0 do
begin
{ computation for x raised to power n}
if (n mod 2)=1 then
product:=product*sequence;
n:=n div 2;
sequence:=sequence*sequence;
end ;
writeln(product );
end
{display the product}
9
ALGORITHM: #8
COMPUTING THE n th FIBONACCI NUMBER
Algorithm description:
1) Establish n, indicating the n th Fibonacci number is required.
2) Derive the binary representation of n by repeated division by 2 and store representation in array
d[1..i-1]
3) Initialize the first two members of the doubling sequence.
4) Stepping down from the (i-1) th most significant digit in the binary representation of n by 1 do
a) use current pair of Fibonacci number fn and fn+1 to generate the par f2n and f2n+1
b) if current binary digit d[k] is zero then make reassignments to fn and fn+1 else extend sequence
by 1 number and then make the reassignments to fn and fn+1
5) return the n th fibonacci number fn.
Algorithm description:
procedure nfibonacci ( var n : integer);
var n,
fn,
d,
i,
fnp1,
f2n,
f2np1,
sqfnp1:integer;
{ n th number for fibonacci number}
{ n th Fibonacci number}
{ array containing binary digits}
{ binary digit count less 1 of n and index of binary array}
{ (n+1) th Fibonacci number}
{ 2n th Fibonacci number}
{square of the (n+1) th Fibonacci number}
begin
i:=0;
while n > 1 do
begin
i:=i+1;
if
odd(n) then d[i] :=1;
else
d[i]:=0
n:=n div 2;
end;
{ generate binary digits for n without the most significant digit}
10
while
fn:=0;
fnp1:=1;
for k := i-1 to 1 do
{generate 2n and (2n+1) th Fibonacci number from n th and (n+1) th}
begin
sqfnp1:=fnp1*fnp1;
f2n:=fn*fn+sqfnp1;
f2np1:=2*fn*fnp1+sqfnp1;
if d[k] :=0 then
begin
fn:=f2n;
fnp1:=f2np1;
end
{ computation for Fibonacci number}
{reassign n th and(n+1) th ready for next doubling phase}
else
begin
fn:=f2np1;
fnp1:=fnp1+f2n;
{ extend sequence by one and reassign nth and (n+1) th}
end
end ;
writeln(fn);
end
{ display the fibonacci number}