Algorithm studying

来源:互联网 发布:伴随矩阵的性质 编辑:程序博客网 时间:2024/05/22 17:12

//Euclid's algorithm

//Euclid(m,n),m>n

//input:two integers m,n which are not less than 0 and not all 0;

//output:the greates common divisor of  m,n

while n!=0 do 

r←m mod n

m←n

n←r

return m

___________________________________________________________________________________________________________________________________


//Sieve of Eratosthenes

//Sieve(n)

//input:an integer n>1

//output:list of primes less than or equal to n

for p←2 to n do A[p]←p 

for p←2 to  √n ┘do

  if A[p]!=0

  j ←p*p

  while j<=n

 A[j]←0

  j←j+p

i←0

for p←2 to n do 

if A[p]!=0

L[i]←A[p]

i←i+1

return L

____________________________________________________________________________________________________________________________________

MatrixMultipication

//input: two n-by-n matrices A and B

//output:Matrix C=AB

for i←0 to n-1 do 

for j←0 to n-1 do

C[i,j]←0.0

for k←0 to n-1 do

C[i,j]←C[i,j]+A[i,k]*B[k,j]

return C

____________________________________________________________________________________________________________________________________






0 0