0% found this document useful (0 votes)
53 views

Simple Algorithm

This document describes three iterative algorithms: one that sums the integers from 1 to N, one that sums the numbers in a list or array, and one that divides a number a into d using long division. The summing algorithms initialize a sum variable, set an index starting at 1 or the list position, add the current term to the sum in a loop while the index is less than or equal to N, and print the final sum. The long division algorithm initializes the first term of a as B1, sets the quotient digit qi as the largest integer such that d times qi is less than or equal to Bi, updates Bi+1 for the next step, and returns the final remainder r.

Uploaded by

MAWiskiller
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Simple Algorithm

This document describes three iterative algorithms: one that sums the integers from 1 to N, one that sums the numbers in a list or array, and one that divides a number a into d using long division. The summing algorithms initialize a sum variable, set an index starting at 1 or the list position, add the current term to the sum in a loop while the index is less than or equal to N, and print the final sum. The long division algorithm initializes the first term of a as B1, sets the quotient digit qi as the largest integer such that d times qi is less than or equal to Bi, updates Bi+1 for the next step, and returns the final remainder r.

Uploaded by

MAWiskiller
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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