Simple Algorithm

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

First Iterative Algorithms

Algorithm Sum the integers from 1 to N

sum ← 0;
x ← 1;
while x ≤ N do
sum ← sum + x;
x ← x + 1;
end while
print sum;

Algorithm Sum N numbers in a list (or array) named values

sum ← 0;
index ← 1;
while index ≤ N do
sum ← sum + values[index ];
index ← index + 1;
end while
print sum;

Algorithm Divide a = a1 a2 · · · aN by d using long division

B1 ← a1 {Bi is what we divide d into in step i}


i←1
while i ≤ N do
qi ← largest integer such that d × qi ≤ Bi ; {qi is the ith digit of the quotient q}
if i ≤ N − 1 then
Bi+1 ← 10 × (Bi − d × qi ) + ai+1
end if
i ← i + 1;
end while
r ← BN − d × qN {r is the remainder}

You might also like