Você está na página 1de 6

Php Database

first secondthird

first third second

secondfirst third

secondthird first

third first second

third secondfirst

The functions pand rare therefore seen to be recursive applications of the function h
with the left argument (number of discs) decremented, and with the right argument (the
posts) permuted.
RECURSION 11
The expression g=: h f.may be used to produce an equivalent function g; the adverb
f.applies to its argument hto (recursively) substitute the referent of each name
encountered, producing a definition in terms of primitives only.
B. TRIANGULAR NUMBERS
The function trn=: +/@Aiuses the function Ai=: 1:+i.(Augmented indices) to produce the triangular
numbers, defined as the number of coins in a packed triangular array
with a specified number of coins in the base row. For example:
(Ai=: 1:+i.) 4
1234

trn 4
10
trn"0 i. 15
0 1 3 6 10 15 21 28 36 45 55 66 78 91 105
Since trn jequalsj+trn j-1, an equivalent recursive definition is:
sr=: 0:`(n + $:@<:)@.*
The function S=: (n*n+1:)%2:given by GKP1.5 (or the equivalent 2:!>:) can be
shown to be equivalent to the recursive definition srby an inductive proof. We offer
instead a proof of the equivalence to S, based on the observation of Gauss cited in GKP;
prefacing it with illustrations of some of the expressions to be used in the proof:
S=: (n*n+1:)%2: Recall that n=: ]
j=: 10
(trn,sr,S) j
55 55 55
|. Ai j
10 9 8 7 6 5 4 3 2 1
(Ai + |.@Ai) j
11 11 11 11 11 11 11 11 11 11
j # j+1
11 11 11 11 11 11 11 11 11 11
+/ j # j + 1
110
j*j+1
110
(j*j+1)%2
55
Proof:
trn j
+/@Ai j Definition of trn

+/@|.@Ai j

+/is symmetric (See E)

-:@(+/@Ai + +/@|.@Ai) j Half sum of equals


-:@(+/)@(Ai + |.@Ai) j Sum distributes over +
-:@(+/)@(] # ] + 1:) j Sum is a list of constants -:@(] * ] + 1:) j Definition of multiplication ((] * ] + 1:)
% 2:) j Definition of -:(halve)
((n * n + 1:) % 2:) j Definition of n S j

Definition of S

C. THE JOSEPHUS PROBLEM


12 Chapter 1
If f=: }:@|.then the repeated application of i&fto an argument x removes items
located at intervals of ifrom those remaining (treated as a circle): f=: }:@|.

Delete last after rotating

x=: 'ABCDE'
x;(3 f x);(3 f 3 f x);(3 f^: 0 1 2 3 x)

ABCDEDEABBDEABCDE
DEAB
BDE
BD

The original Josephus problem as presented in GKP concerns the application of 3&fto
the argument Ai 41(the positions of 41 men formed in a circle) until only two (that is,
one less than the interval) survive. To effect this we define and use the following
function:
js =: f^:(1: + #@] - [)
3 js Ai 41
16 31
We will concentrate (as does GKP) on the case of aninterval of two, and therefore define
a function jthat is equivalent to jsexcept that it has a monadic case 2&jsand ranks 0
1:
j=: 2&$: : js " 0 1

(j x);(,j\x);(,j\1+i.# x);(,j\i.# x)

CAACAC1 1 3 1 30 0 2 0 2

,j\Ai 16
1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1
< ;. 1 ,j\Ai 16 Box cut on leading item. See E.

11 31 3 5 71 3 5 7 9 11 13 151

The last two results agree with the table of values of j that appears after GKP1.8.
However, a more obvious pattern is provided by labelling the positions with indices
beginning at zero rather than one, and we will continue to use such zero-origin indexing
hereafter:
< ;. 1 ,j\i. 16

00 20 2 4 60 2 4 6 8 10 12 140

This result leads to a recursive definition thatdiffers somewhat from that of GKP1.8, but
can easily be related to it:
jr=: 0:`even`odd @. c
even=: +:@>:@jr@<:@-:
odd=: +:@jr@-:@<:
c=: * * >:@(2&|)
b ,: jr"0 b=: i. 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 0 2 0 2 4 6 0 2 4 6 8 10 12 14 0 2 4 6 8
<;.1 jr"0 b

00 20 2 4 60 2 4 6 8 10 12 140 2 4 6 8

RECURSI

Você também pode gostar