Pp Pascal (#84)

How about something like: http://rephorm.com/files/dump/pascal28-2.txt
(Easier to view in a web browser than email client / terminal)

I error to the left when aligning on the left side of the triangle, and
to the right on the right side.

On Sat, Jun 24, 2006 at 04:41:52AM +0900, Erik V. wrote:

I error to the left when aligning on the left side of the
triangle, and to the right on the right side.

No, you don’t… ;]

The left “picture” formed by the numbers 3, 10, 35 and 126
should be a mirror of the right one. It isn’t…

But I like the idea of “erroring” to opposite sides. What about
the center column?

Oops :slight_smile:
Thanks. I was off by one on the midpoint.
Update: http://rephorm.com/files/dump/pascal28-3.txt

Since the center column is arbitrary, I just have it erroring to the
right. (It is past the floor of the rows center point).

Brian

I error to the left when aligning on the left side of the
triangle, and to the right on the right side.

No, you don’t… ;]

The left “picture” formed by the numbers 3, 10, 35 and 126
should be a mirror of the right one. It isn’t…

But I like the idea of “erroring” to opposite sides. What about
the center column?

gegroet,
Erik V. - http://www.erikveen.dds.nl/

To me it meant:

  1. Find the longest number in the result set.
  2. Center all numbers in a field that size.
  3. Find the length of the last line.
  4. Center all lines at that length.
    Yes, but you can never center a 2 bytes long string on 3 bytes
    unfortunately, and that’s what bugged me… However, I found a
    solution that will finally allow me to sleep well at night :))

Now, if I can decide what it means to me, that’s fine by me :slight_smile:

I promise, I wont come and take your keyboard away, no matter what
you decide. :wink:
You couldn’t anyway, the cat sits on it and bites everyone who comes
close to it… Somehow I thought she’d go for the mouse when I bough
her… I was wrong :))

Have a pleasant evening everyone,
Alex

[email protected] wrote:

Oops :slight_smile:
Thanks. I was off by one on the midpoint.
Update: http://rephorm.com/files/dump/pascal28-3.txt

Since the center column is arbitrary, I just have it erroring to the
right. (It is past the floor of the rows center point).

Brian

looks realy nice.

i just stumbled about something interesting. Instead of
outputting the number just put a ‘*’ for odd numbers and a space for
even ones. Try with high row count (at least 32)…

Fun quiz btw.

cheers

Simon

On Sat, 24 Jun 2006 00:00:22 +0200, Simon Kröger [email protected]
wrote:

the center column?

looks realy nice.

i just stumbled about something interesting. Instead of
outputting the number just put a ‘*’ for odd numbers and a space for
even ones. Try with high row count (at least 32)…

Wow, very interesting!

It also works for other numbers (primes in particular) instead of just
2,
e.g. space if number is divisible by 5 and “*” if not.

Fun quiz btw.

Indeed.

Dominik

But I like the idea of “erroring” to opposite sides. What
about the center column?

I added erroring to the left on the left hand side and erroring
to the right on the right hand side to my code. Looks nice.

left = !right = cellnr > row.length/2

Sometime Ruby makes me smile. I mean, left isn’t right, is it?

If I run it in $DEBUG mode, its output looks like the pictures
below. It clearly shows “cells”, “seperators” and “margins”.

gegroet,
Erik V. - http://www.erikveen.dds.nl/


$ pascaltriangle 8

.1.<<<<<<<<<<<<<
.1. .1.<<<<<<<<<<<
.1. .2. .1.<<<<<<<<<
.1. .3. .3. .1.<<<<<<<
.1. .4. .6. .4. .1.<<<<<
.1. .5. .10 10. .5. .1.<<<
.1. .6. .15 20. 15. .6. .1.<

  1. .7. .21 .35 35. 21. .7. .1

$ pascaltriangle 12

.1.<<<<<<<<<<<<<<<<<<<<<
.1. .1.<<<<<<<<<<<<<<<<<<<
.1. .2. .1.<<<<<<<<<<<<<<<<<
.1. .3. .3. .1.<<<<<<<<<<<<<<<
.1. .4. .6. .4. .1.<<<<<<<<<<<<<
.1. .5. .10 10. .5. .1.<<<<<<<<<<<
.1. .6. .15 20. 15. .6. .1.<<<<<<<<<
.1. .7. .21 .35 35. 21. .7. .1.<<<<<<<
.1. .8. .28 .56 70. 56. 28. .8. .1.<<<<<
.1. .9. .36 .84 126 126 84. 36. .9. .1.<<<
.1. .10 .45 120 210 252 210 120 45. 10. .1.<

  1. .11 .55 165 330 462 462 330 165 55. 11. .1

Did you know that you need only 36 bytes of Ruby code to
build the next row, based on the previous row? Really!

I’ve got 32 bytes here :slight_smile:

That was an easy one: map is 4 characters shorter than collect.

But I was able to strip one more byte… It’s now 31 bytes.

Sorry…

gegroet,
Erik V. - http://www.erikveen.dds.nl/

PS: Hint: << instead of +

On 24/06/2006, at 9:51 AM, Erik V. wrote:

Did you know that you need only 36 bytes of Ruby code to
build the next row, based on the previous row? Really!

I’ve got 32 bytes here :slight_smile:

That was an easy one: map is 4 characters shorter than collect.

But I was able to strip one more byte… It’s now 31 bytes.

Damn. I’m stuck at 37. This is going to drive me nuts all day. :slight_smile:

Pete Y.
http://9cays.com/

On Sat, Jun 24, 2006 at 12:23:58PM +0900, Pete Y. wrote:

Damn. I’m stuck at 37. This is going to drive me nuts all day. :slight_smile:

Pete Y.
http://9cays.com/

Does that 31 bytes include storing the result of the statement?
I can get 31 bytes that will return the next row (in irb, e.g), but the
result is thrown away.

Now, given an ‘n’ that stores the number of rows to calculate, how much
does it take to build the entire triangle? (e.g. an array of arrays
stored in ‘a’).

I’ve got it at 56 characters.

On 23/06/06, Erik V. [email protected] wrote:

Did you know that you need only 36 bytes of Ruby code to build
the next row, based on the previous row? Really!

I’ve got 32 bytes here :slight_smile:

Paul.

Does that 31 bytes include storing the result of the
statement?

No, not storing, just building. The right hand side of the
assignment. Storing will cost you 2 extra bytes.

Now, given an ‘n’ that stores the number of rows to
calculate, how much does it take to build the entire
triangle? (e.g. an array of arrays stored in ‘a’).

I’ve got it at 56 characters.

Building the triangle takes 54 bytes.

(Storing it will cost you 2 extra bytes. The question is: Do
you want to store it?)

gegroet,
Erik V. - http://www.erikveen.dds.nl/

On 24/06/06, Erik V. [email protected] wrote:

Building the triangle takes 54 bytes.

(Storing it will cost you 2 extra bytes. The question is: Do
you want to store it?)

If you don’t want to store it, and predefine a certain function (which
means that you no longer need to store it, incidentally), the entire
triangle can be calculated in 37 bytes. However, that makes the
overall program longer…

Paul.

Building the triangle takes 54 bytes.

(Storing it will cost you 2 extra bytes. The question is:
Do you want to store it?)

If you don’t want to store it, and predefine a certain
function (which means that you no longer need to store it,
incidentally), the entire triangle can be calculated in 37
bytes. However, that makes the overall program longer…

Personally, I don’t build the triangle at all. I mean, I don’t
store the whole triangle in any place.

If you try to remember the whole triangle, you’d need a lot of
memory. If you iterate over the rows, building the next one
based on the previous one and forgetting the previous one
instantly, can be done in a (memory-) cheap way (with just 31
bytes or Ruby code…).

The only problem with this is that you need to know the maximum
cell width (on the last row) when you build the first one. So I
iterate twice over all rows. The first time to determine the
last row and its biggest cell and the second time to format and
print each row.

In this situation, the maximum number of cells in memory at any
time, is roughly twice the number of cells on the last row,
more exactly N2-1 (which could probably be reduced to N or
even N/2+1). When you try to remember the whole tree, you need
to remember N
(N+1)/2 cells, or about N^2/2 cells.

My point is: Space is limited by nature, Time isn’t. Or, in the
world of programming: It’s usually better to eat CPU cycles
than MB’s. Especially when you think big…

I don’t say that it’s faster, but you can simply build bigger
triangles.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

Building the triangle takes 54 bytes.

(Storing it will cost you 2 extra bytes. The question is:
Do you want to store it?)

If you don’t want to store it, and predefine a certain
function (which means that you no longer need to store it,
incidentally), the entire triangle can be calculated in 37
bytes. However, that makes the overall program longer…

Personally, I don’t build the triangle at all. I mean, I don’t
store the whole triangle in any place.

If you try to remember the whole triangle, you’d need a lot of
memory. If you iterate over the rows, building the next one
based on the previous one and forgetting the previous one
instantly, can be done in a (memory-) cheap way (with just 31
bytes or Ruby code…).

The only problem with this is that you need to know the maximum
cell width (on the last row) when you build the first one. So I
iterate twice over all rows. The first time to determine the
last row and its biggest cell and the second time to format and
print each row.

In this situation, the maximum number of cells in memory at any
time, is roughly twice the number of cells on the last row,
more exactly N2-1 (which could probably be reduced to N or
even N/2+1). When you try to remember the whole tree, you need
to remember N
(N+1)/2 cells, or about N^2/2 cells.

My point is: Space is limited by nature, Time isn’t. Or, in the
world of programming: It’s usually better to eat CPU cycles
than MB’s. Especially when you think big…

I don’t say that it’s faster, but you can simply build bigger
triangles.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

Erik V. wrote:

My point is: Space is limited by nature, Time isn’t. Or, in the
world of programming: It’s usually better to eat CPU cycles
than MB’s. Especially when you think big…

I don’t say that it’s faster, but you can simply build bigger
triangles.

Not a spoiler, but a hint from someone who learned about Pascal’s
Triangle in the 9th grade: what would change if you knew how long the
last row would be before you started printing the first one?

As Dijkstra said, “The purpose of thinking is to reduce the amount of
detailed work to a doable amount.”

--

M. Edward (Ed) Borasky

Erik V. wrote:

My point is: Space is limited by nature, Time isn’t. Or, in the
world of programming: It’s usually better to eat CPU cycles
than MB’s. Especially when you think big…

I’d qualify this statement. This is subject to the whim of the user. The
user might consider time as limited as well. Especially if the job takes
super long, in the eyes of the user.

Anyways, I’m really enjoying this quiz. It’s my first attempt at one of
these. It is seemingly very straight-forward as to what is needed and
how to get there, but there are so many ways of approaching the problem.
I look forward to seeing how others have gone about solving this one.

On Sun, Jun 25, 2006 at 12:03:17AM +0900, Erik V. wrote:

The only problem with this is that you need to know the maximum
cell width (on the last row) when you build the first one. So I
iterate twice over all rows. The first time to determine the
last row and its biggest cell and the second time to format and
print each row.

Its fairly easy to calculate the maximum digit in the triangle without
calculating all the rows (basically O(N)).

(think binomial coefficients)

The only problem with this is that you need to know the
maximum cell width (on the last row) when you build the
first one. So I iterate twice over all rows. The first time
to determine the last row and its biggest cell and the
second time to format and print each row.

Its fairly easy to calculate the maximum digit in the
triangle without calculating all the rows (basically O(N)).

I found a pretty good explanation on [1].

I’m not a mathematician, but I know how to use this
information. So, since we calculate the biggest number on the
last row before we start, it’s possible to iterate over all
rows only once. There’s absolutely no need to remember the
whole triangle. Good.

Thanks.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

[1] Mathwords: Binomial Coefficients in Pascal's Triangle

The only problem with this is that you need to know the maximum
cell width (on the last row) when you build the first one. So I
iterate twice over all rows. The first time to determine the
last row and its biggest cell and the second time to format and
print each row.

You really don’t need to build the whole triangle to know the length of
the
biggest number… I don’t think I’m allowed to give you the formula,
but it
should’nt be that hard to figure out anyway :wink: