140
Proofs, Induction, and Number Theory
18. Function to return the value 2
n
for n ≥ 1.
TwosPower (positive integer n)
Local variables:
integers i, j
i = 1
j = 2
while i ∙ n do
j = j ∗ 2
i = i + 1
end while
//j now has the value 2
n
return j
end function TwosPower
19. Function to return the value x ∗ n! for n ≥ 1.
AnotherOne (integer x; positive integer n)
Local variables:
integers i, j
i = 1
j = x
while i ∙ n do
j = j ∗ (i + 1)
i = i + 1
end while
//j now has the value x ∗ n!
return j
end function AnotherOne
20. Function to return the value of the polynomial a n x
n
+ a n–1 x
n–1
+ g+ a 1 x + a 0 at a given value of x.
Polly (real a n ; … ; real a 0 ; real x)
Local variables:
integers i, j
i = n
j = a n
while i ∙ 0 do
j = j ∗ x + a i–1
i = i – 1
end while
//j now has value of the polynomial evaluation
return j
end function Polly
21. Function to return the maximum value from the first n entries a[1], a[2], … , a[n], n ≥ 1, in an array of
distinct integers.
ArrayMax (integers n, a[1], a[2], … , a[n])
Local variables:
integers i, j
i = 1
Précédent

- 157/986

Suivant