* Pascal triangle algorithm ("homework"). iterative one. the direct one was simple.
@ 2021-01-30 14:40 Mehdi Saada
2021-01-30 16:36 ` DrPi
0 siblings, 1 reply; 4+ messages in thread
From: Mehdi Saada @ 2021-01-30 14:40 UTC (permalink / raw)
Here's this, complete with put_line and comments...
Sorry for the length. I can't even follow anymore where's it going wrong and why. tried to. At least the "swap" routine does what it 's supposed to.
I hope you'll enlighten me.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Pascal is
type Tab_type is array(Natural range <>) of Natural;
function Factorial (Number: Natural) return Positive is
result: Positive := 1;
begin
for I in 1..Number loop
result := Result * I;
end loop;
return result;
end Factorial;
function Give_Pascal_factoriel (N_rank: in Natural) return Tab_type is
Result: Tab_type(0..N_rank);
begin
if N_rank+1 mod 2 = 0 then
for I in 0..N_rank/2 loop
Result(I) := Factorial(N_rank) / (Factorial(N_rank-I)*Factorial(I));
end loop;
Result(N_rank/2+1..N_rank) := Result(0..N_rank/2);
else
for I in 0..N_rank/2 loop
Result(I) := Factorial(N_rank) / (Factorial(N_rank-I)*Factorial(I));
Result(N_rank-I) := Result(I);
end loop;
end if;
return Result;
end Give_Pascal_factoriel;
function Give_Pascal_iterative (N_rank: in Natural) return Tab_type is
Temp1, Temp2: Tab_type(0..N_rank+2):= (1 => 1, others => 0);
Index1, Index2: Natural;
procedure Swap(Temp2, Temp1: in out Tab_type;Index2, Index1: in out Natural) is
temp_vrai_index : Natural := index1;
Temp_vrai: Tab_type := temp1;
begin
temp1(0..index2) := temp2(0..Index2);
index1 := index2;
temp2 := Temp_vrai;
index2 := temp_vrai_index;
end Swap;
begin
if N_rank = 0 then return Tab_type'(0=> 1);
elsif N_rank = 1 then return (1,1);
else
temp1(0..2) := (0,1,1);
index1 := 2;
-- each instance of this loop creates a new line on array 2 from array1
-- then arrays are swapped.
for N in 1..N_rank loop
-- each instance of the inner loop fill one slot of array2 up to N/2, and reciprocally on the other side of the array
-- then in case the number of coefficients for the nth line is even
-- the last one at place N/2+1 (considering the array starts at 0);
for I in 1..N/2 loop
Put_Line("I = " & I'Image);
Put_Line("temp1(I) : " & temp1(I)'Image);
Put_Line("temp1(I-1): " & temp1(I-1)'Image);
temp2(I) := temp1(I-1) + temp1(I);
Put_Line("temp2((I)'Image is now :" & temp2(I)'Image);
end loop;
Put_Line("temp2 :" );
for A of temp2(1..N/2) loop
Put_Line(A'Image);
end loop;
if N mod 2 = 0 then
Put_Line("N = " & N'Image & " so number of coefficients is odd");
temp2(N/2+1) := temp1(N/2) + temp1(N/2+1);
end if;
Index2 := index1 + 1;
Swap(Index2 => Index2, Index1 => Index1,
Temp2 => Temp2, Temp1 => Temp1);
-- index1 and index2 serve only to swap the arrays,
-- whose size is constant and put to the max they'll have to be.
end loop;
end if;
return Temp2(1..N_rank+2);
end Give_Pascal_iterative;
tab_result : Tab_type(0..6);
begin
tab_result := Give_Pascal_iterative(N_rank => 5);
New_Line;
Put_Line("attempt");
for A of tab_result loop
Put_Line(A'Image);
end loop;
end Pascal;
Listing: /bin/sh -c /home/mehdi/obj/pascal
temp2 :
I = 1
temp1(I) : 1
temp1(I-1): 0
temp2((I)'Image is now : 1
temp2 :
1
N = 2 donc nombre de cefficients impairs
I = 1
temp1(I) : 1
temp1(I-1): 0
temp2((I)'Image is now : 1
temp2 :
1
I = 1
temp1(I) : 1
temp1(I-1): 0
temp2((I)'Image is now : 1
I = 2
temp1(I) : 0
temp1(I-1): 1
temp2((I)'Image is now : 1
temp2 :
1
1
N = 4 donc nombre de cefficients impairs
I = 1
temp1(I) : 1
temp1(I-1): 0
temp2((I)'Image is now : 1
I = 2
temp1(I) : 1
temp1(I-1): 1
temp2((I)'Image is now : 2
temp2 :
1
2
essai
1
1
0
0
0
0
0
[2021-01-30 14:34:20] process terminated successfully, elapsed time: 01.04s
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Pascal triangle algorithm ("homework"). iterative one. the direct one was simple.
2021-01-30 14:40 Pascal triangle algorithm ("homework"). iterative one. the direct one was simple Mehdi Saada
@ 2021-01-30 16:36 ` DrPi
2021-01-30 16:55 ` Mehdi Saada
0 siblings, 1 reply; 4+ messages in thread
From: DrPi @ 2021-01-30 16:36 UTC (permalink / raw)
Le 30/01/2021 à 15:40, Mehdi Saada a écrit :
> Here's this, complete with put_line and comments...
> Sorry for the length. I can't even follow anymore where's it going wrong and why. tried to. At least the "swap" routine does what it 's supposed to.
> I hope you'll enlighten me.
>
To get an answer, you have to describe your problem.
Posting code and console output is not enough.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Pascal triangle algorithm ("homework"). iterative one. the direct one was simple.
2021-01-30 16:36 ` DrPi
@ 2021-01-30 16:55 ` Mehdi Saada
2021-01-30 23:42 ` Mehdi Saada
0 siblings, 1 reply; 4+ messages in thread
From: Mehdi Saada @ 2021-01-30 16:55 UTC (permalink / raw)
> To get an answer, you have to describe your problem.
> Posting code and console output is not enough.
Sorry. well it should output the n_rank'th (here 6th, but in general as given in the argument) row of Pascal's triangle, through iterating over each row until the required one.
there are two loops.
first one, the main one, change rows after filling them at each iteration, then swap temp1( n-1 row) and temp2 (nth row).
then the second ,inner loop, where apparently the problem is. Here it loops over a single row, by filling each element of the nth row one at a time by adding the nth and (n-1)th element of the preceding row. It does compute it for the first second first loop iteration as shown by the verification (temp2(i) = temp1(i-1) + temp1(i) = 2)
but from there, since the swap routine works, I don't understand why it does not carry one.
temp2 :
1
2
why from there can't it continue, with temp2(3) := temp1(2) + temp1(3) [ = 1 + 0
... well now I don't remember whether or not I put a 0 at the end of each row. But If I didn't it should crash ?
Unless N should be incremented an the loop continues one further...
I'll test that... once GNAT works again, as it crashes twice... two different versions, as explained elsewhere. Not my fault, for once.
I hope I explained better.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Pascal triangle algorithm ("homework"). iterative one. the direct one was simple.
2021-01-30 16:55 ` Mehdi Saada
@ 2021-01-30 23:42 ` Mehdi Saada
0 siblings, 0 replies; 4+ messages in thread
From: Mehdi Saada @ 2021-01-30 23:42 UTC (permalink / raw)
I did it, it works just fine, for much bigger exponents than with the direct computation algorithm. As expected. I could do it without help, nor looking on the internet. Pretty proud and... it's pretty cute seeing the triangle.
Somehow working out that here helped me a great deal... Often does that !
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-01-30 23:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-30 14:40 Pascal triangle algorithm ("homework"). iterative one. the direct one was simple Mehdi Saada
2021-01-30 16:36 ` DrPi
2021-01-30 16:55 ` Mehdi Saada
2021-01-30 23:42 ` Mehdi Saada
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox