Velocity Reviews > Ruby > Translating A Pattern of Data Into Equation, and ultimately code

# Translating A Pattern of Data Into Equation, and ultimately code

Zach Dennis
Guest
Posts: n/a

 04-10-2005
I have the following table of data, and I am looking to create an
equation to recreate the table. If anyone finds this sort of thing fun,
or has a online resource or a good book that helps explaining these
types of math concepts, please take a stab at it or post any info that
might aide me in my discovery! I've been trying the past several hours
and this sort of thing isn't my strong suit...and i'm stuck!

I am first looking to understand how-to make the pattern of data into a
actual math equation...then from there I'd like to write ruby code for
the equation.

Here is the sample data in a table:
- rows represent a different character (as labeled)
- columns represent the position of the character
(examples represent data in decimal notation, not octal/hex)
example: "aaaa" would become 000 003 002 005
example: "zach" would become 027 003 000 012

1 2 3 4 5 6 (etc......)
----------------------------------------------------
a 1 | 0 3 2 5 4 7
b 2 | 3 0 1 6 7 4
c 3 | 2 1 0 7 6 5
d 4 | 5 6 7 0 1 2
e 5 | 4 7 6 1 0 3
f 6 | 7 4 5 2 3 0
g 7 | 6 5 4 3 2 1
h 8 | 9 10 11 12 13 14
i 9 | 8 11 10 13 12 15
j 10 | 11 8 9 14 15 12
k 11 | 10 9 8 15 14 13
l 12 | 13 14 15 8 9 10
m 13 | 12 15 14 9 8 11
n 14 | 15 12 13 10 11 8
o 15 | 14 13 12 11 10 9
p 16 | 17 18 19 20 21 22
q 17 | 16 19 18 21 20 23
r 18 | 19 16 17 22 23 20
s 19 | 18 17 16 23 22 21
t 20 | 21 22 23 16 17 18
u 21 | 20 23 22 17 16 19
v 22 | 23 20 21 18 19 16
w 23 | 22 21 20 19 18 17
x 24 | 25 26 27 28 29 30
y 25 | 24 27 26 29 28 31
z 26 | 27 24 25 30 31 28

Thanks again if you decide to attempt this...

Zach

Phil Tomson
Guest
Posts: n/a

 04-11-2005
In article <(E-Mail Removed)>,
Zach Dennis <(E-Mail Removed)> wrote:
>I have the following table of data, and I am looking to create an
>equation to recreate the table. If anyone finds this sort of thing fun,
>or has a online resource or a good book that helps explaining these
>types of math concepts, please take a stab at it or post any info that
>might aide me in my discovery! I've been trying the past several hours
>and this sort of thing isn't my strong suit...and i'm stuck!
>
>I am first looking to understand how-to make the pattern of data into a
>actual math equation...then from there I'd like to write ruby code for
>the equation.
>
>Here is the sample data in a table:
> - rows represent a different character (as labeled)
> - columns represent the position of the character
> (examples represent data in decimal notation, not octal/hex)
> example: "aaaa" would become 000 003 002 005
> example: "zach" would become 027 003 000 012
>
> 1 2 3 4 5 6 (etc......)
>----------------------------------------------------
>a 1 |0 3 2 5 4 7
>b 2 |3 0 1 6 7 4
>c 3 |2 1 0 7 6 5
>d 4 |5 6 7 0 1 2
>e 5 |4 7 6 1 0 3
>f 6 |7 4 5 2 3 0
>g 7 |6 5 4 3 2 1
>h 8 |9 10 11 12 13 14
>i 9 |8 11 10 13 12 15
>j 10 |11 8 9 14 15 12
>k 11 |10 9 8 15 14 13
>l 12 |13 14 15 8 9 10
>m 13 |12 15 14 9 8 11
>n 14 |15 12 13 10 11 8
>o 15 |14 13 12 11 10 9
>p 16 |17 18 19 20 21 22
>q 17 |16 19 18 21 20 23
>r 18 |19 16 17 22 23 20
>s 19 |18 17 16 23 22 21
>t 20 |21 22 23 16 17 18
>u 21 |20 23 22 17 16 19
>v 22 |23 20 21 18 19 16
>w 23 |22 21 20 19 18 17
>x 24 |25 26 27 28 29 30
>y 25 |24 27 26 29 28 31
>z 26 |27 24 25 30 31 28

I think the table got a bit shifted so I shifted it back...

What's the application for this or is it just a brain teaser?

Just a few ideas:

For character 'a' it looks like the 'formula' would be something like:
if(position.odd?)
return position - 1
else
return position + 1
end

(BTW: it also looks like the numbers in column 1 follow this same
pattern)

For character 'b' it's a bit stranger, recreating that row of the table:

> 1 2 3 4 5 6 (7) (
>----------------------------------------------------
>b 2 |3 0 1 6 7 4 5 10

(1+2) (2-2) (3-2) (4+2) (5+2) (6-2) (7-2) (8+2)

I'm guessing the 7 and 8 columns will follow the same pattern.

Also, I'd look into the idea that maybe these are modulo-n counters (?)
However, if I look at the row numbers I can see that they directly map
onto columns as well, look at row 'c' and column 3:

1 2 3 4 5 6 (etc......)
----------------------------------------------------
2
1
c 3 |2 1 0 7 6 5
7
6
5

Phil

Csaba Henk
Guest
Posts: n/a

 04-11-2005
On 2005-04-10, Zach Dennis <(E-Mail Removed)> wrote:
> I have the following table of data, and I am looking to create an
> equation to recreate the table. If anyone finds this sort of thing fun,
> or has a online resource or a good book that helps explaining these
> types of math concepts, please take a stab at it or post any info that
> might aide me in my discovery! I've been trying the past several hours
> and this sort of thing isn't my strong suit...and i'm stuck!
>
> I am first looking to understand how-to make the pattern of data into a
> actual math equation...then from there I'd like to write ruby code for
> the equation.
>
> Here is the sample data in a table:
> - rows represent a different character (as labeled)
> - columns represent the position of the character
> (examples represent data in decimal notation, not octal/hex)
> example: "aaaa" would become 000 003 002 005
> example: "zach" would become 027 003 000 012
>
> 1 2 3 4 5 6 (etc......)
> ----------------------------------------------------
> a 1 | 0 3 2 5 4 7
> b 2 | 3 0 1 6 7 4
> c 3 | 2 1 0 7 6 5

OK I see how you turn "zach" into 027 003 000 012.

But not more. Is this table filled with random/real-life data or is
there some rule I don't get? What is it exactly that you want an
equation for?

Do you really want an equation or just an expression?

FYI, "x**2 + 2" is an expression, "x**2 + 2 = 13" is an equation (ie.,
equations are predicative, like "I go fishing tomorrow", expressions are
descriptive, like "the picture on the wall".

Csaba

Zach Dennis
Guest
Posts: n/a

 04-11-2005
Phil Tomson wrote:

> I think the table got a bit shifted so I shifted it back...
>
> What's the application for this or is it just a brain teaser?

WebSTAR, is a Web/Email/Ftp/etc... server that runs on OSX. The Ftp
portion hasn't been updated in several years, and the password is
encrypted for each user. Users have to be added/removed via a GUI right
now, so I am writing a command line ruby program to add and remove
accounts from the user data file. I have everything except for the
password encryption part done. I struggled with trying to decipher it
myself, and was hoping someone better in this area of math be able to
help me out. My command line utility will be open source and donated to
everyone and especially the WebSTAR community.

> Also, I'd look into the idea that maybe these are modulo-n counters (?)

This was what I was looking into, but I couldn't find one that would
consistently work for multiple rows/columns. I am/was thinking that
there would be a expression that could be used across the board.

> However, if I look at the row numbers I can see that they directly map
> onto columns as well, look at row 'c' and column 3:
>
> 1 2 3 4 5 6 (etc......)
> ----------------------------------------------------
> 2
> 1
> c 3 |2 1 0 7 6 5
> 7
> 6
> 5
>

Thanks thus far Phil. If you have any other ideas, please feel free to
post em.

Zach

Zach Dennis
Guest
Posts: n/a

 04-11-2005
Csaba Henk wrote:

> OK I see how you turn "zach" into 027 003 000 012.
>
> But not more. Is this table filled with random/real-life data or is
> there some rule I don't get? What is it exactly that you want an
> equation for?
>

It is real life data pulled straight from a data file. See previous
response to Phil Thomson's post for what the data file is used for.

When passwords are stored the characters are stored based on their
position in the password. I was hoping there would be an expression that
could be used to create the table.

> Do you really want an equation or just an expression?
>
> FYI, "x**2 + 2" is an expression, "x**2 + 2 = 13" is an equation (ie.,
> equations are predicative, like "I go fishing tomorrow", expressions are
> descriptive, like "the picture on the wall".

I would like an expression that can be used to recreate the table i
posted before, if i have my understanding right of expression and
equation. I'd like to say:
row - col * (col-1)

so i could fill in the row/col variables and recreate my table...Thanks
for posting Csaba. Any other insight would be very appreciated.

Zach

Edgardo Hames
Guest
Posts: n/a

 04-11-2005
On Apr 10, 2005 8:45 PM, Zach Dennis <(E-Mail Removed)> wrote:
> I have the following table of data, and I am looking to create an
> equation to recreate the table. If anyone finds this sort of thing fun,
> or has a online resource or a good book that helps explaining these
> types of math concepts, please take a stab at it or post any info that
> might aide me in my discovery! I've been trying the past several hours
> and this sort of thing isn't my strong suit...and i'm stuck!
>
> I am first looking to understand how-to make the pattern of data into a
> actual math equation...then from there I'd like to write ruby code for
> the equation.
>
> Here is the sample data in a table:
> - rows represent a different character (as labeled)
> - columns represent the position of the character
> (examples represent data in decimal notation, not octal/hex)
> example: "aaaa" would become 000 003 002 005
> example: "zach" would become 027 003 000 012
>
> 1 2 3 4 5 6 (etc......)
> ----------------------------------------------------
> a 1 | 0 3 2 5 4 7
> b 2 | 3 0 1 6 7 4
> c 3 | 2 1 0 7 6 5
> d 4 | 5 6 7 0 1 2
> e 5 | 4 7 6 1 0 3
> f 6 | 7 4 5 2 3 0
> g 7 | 6 5 4 3 2 1
> h 8 | 9 10 11 12 13 14
> i 9 | 8 11 10 13 12 15
> j 10 | 11 8 9 14 15 12
> k 11 | 10 9 8 15 14 13
> l 12 | 13 14 15 8 9 10
> m 13 | 12 15 14 9 8 11
> n 14 | 15 12 13 10 11 8
> o 15 | 14 13 12 11 10 9
> p 16 | 17 18 19 20 21 22
> q 17 | 16 19 18 21 20 23
> r 18 | 19 16 17 22 23 20
> s 19 | 18 17 16 23 22 21
> t 20 | 21 22 23 16 17 18
> u 21 | 20 23 22 17 16 19
> v 22 | 23 20 21 18 19 16
> w 23 | 22 21 20 19 18 17
> x 24 | 25 26 27 28 29 30
> y 25 | 24 27 26 29 28 31
> z 26 | 27 24 25 30 31 28
>

First, I transposed the table since the text editor works best across lines not
columns I looked at the deltas between the columns and the natural sequence
of numbers and I found an interesting pattern. I will attempt to give you an
explanation of what I came up with, and I hope it's in any way helpful.

For columns numbered with a power of two (1, 2, 4, 8, 16, ...) the deltas are
the power of two alternating its sign +/-.
It's rather difficult to explain, so please, take a look at the following table:

Input 1 2 3 4 5 6 7 8 9 10 11 12
-----------------------------------
Col 1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1
Col 2 +2 -2 -2 +2 +2 -2 -2 +2 -2 -2 +2 +2
Col 4 +4 +4 +4 -4 -4 -4 -4 +4 +4 +4 +4 -4
Col 8 +8 +8 +8 +8 +8 +8 +8 -8 -8 -8 -8 -8

The sign is positive until you reach the given power of two, then it changes to
negative for that power of two times and will keep changing back and forth (it
may help you trying to complete some other values on my table, since I don't
know whether I make myself clear).

The values for the deltas on the other columns is a combination of these
previous columns (just like any number can be expressed as a sum of some powers
of two) For example, on column 3, the delta equals the delta on col 1 plus the
delta on col 2. Take a look:

Col 1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1
Col 2 +2 -2 -2 +2 +2 -2 -2 +2 -2 -2 +2 +2
-----------------------------------------------
Col 3 +1 -1 -3 +3 +1 -1 -3 +3 -3 -1 +1 +3

Column 5 can be calculated based on col 1 + col 4 (5 = 0101)

Col 1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1
Col 4 +4 +4 +4 -4 -4 -4 -4 +4 +4 +4 +4 -4
-----------------------------------------------
Col 5 +3 +5 +3 -3 -5 -3 -5 +5 +3 +5 +3 -3

Column 6 can be calculated based on col 2 + col 4 (6 = 0110)

Col 2 +2 -2 -2 +2 +2 -2 -2 +2 -2 -2 +2 +2
Col 4 +4 +4 +4 -4 -4 -4 -4 +4 +4 +4 +4 -4
-----------------------------------------------
Col 6 +6 +2 +2 -2 -2 -6 -6 +6 +2 +2 +6 -2

Column 7 can be calculated based on col 1 + col 2 + col 4 (7 = 0111)

Col 1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1
Col 2 +2 -2 -2 +2 +2 -2 -2 +2 -2 -2 +2 +2
Col 4 +4 +4 +4 -4 -4 -4 -4 +4 +4 +4 +4 -4
-----------------------------------------------
Col 7 +5 +3 +1 -1 -3 -5 -7 +7 +1 +3 +5 -1

Column 9 is no exception. Take column 1 and 8 and add them (9 = 1001)

Col 1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1
Col 8 +8 +8 +8 +8 +8 +8 +8 -8 -8 -8 -8 -8
-----------------------------------------------
Col 9 +7 +9 +7 +9 +7 +9 +7 -7 -9 -7 -9 -7

I guess you can write a formula for the table based on this
explanation (which I hope was clear enough). If you need further
assistance, I'll be keeping an eye on this thread. I think it was an
interesting problem, very well suited for a Ruby Quiz

Kind regards,
Ed
--
Encontrá a "Tu psicópata favorito" http://tuxmaniac.blogspot.com

linus sellberg
Guest
Posts: n/a

 04-11-2005
Zach Dennis wrote:
> I have the following table of data, and I am looking to create an
> equation to recreate the table. If anyone finds this sort of thing fun,

You might want to look into

http://planetmath.org/encyclopedia/B...Algorithm.html

Martin DeMello
Guest
Posts: n/a

 04-11-2005
Edgardo Hames <(E-Mail Removed)> wrote:
>
> Input 1 2 3 4 5 6 7 8 9 10 11 12
> -----------------------------------
> Col 1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1
> Col 2 +2 -2 -2 +2 +2 -2 -2 +2 -2 -2 +2 +2
> Col 4 +4 +4 +4 -4 -4 -4 -4 +4 +4 +4 +4 -4
> Col 8 +8 +8 +8 +8 +8 +8 +8 -8 -8 -8 -8 -8

Note that if you read down the columns, this gives you each number in
binary, using - for 1 and + for 0

martin

Csaba Henk
Guest
Posts: n/a

 04-11-2005
On 2005-04-11, Edgardo Hames <(E-Mail Removed)> wrote:
> On Apr 10, 2005 8:45 PM, Zach Dennis <(E-Mail Removed)> wrote:
> For columns numbered with a power of two (1, 2, 4, 8, 16, ...) the deltas are
> the power of two alternating its sign +/-.
> It's rather difficult to explain, so please, take a look at the following table:
>
> Input 1 2 3 4 5 6 7 8 9 10 11 12
> -----------------------------------
> Col 1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1 -1 +1
> Col 2 +2 -2 -2 +2 +2 -2 -2 +2 -2 -2 +2 +2
> Col 4 +4 +4 +4 -4 -4 -4 -4 +4 +4 +4 +4 -4
> Col 8 +8 +8 +8 +8 +8 +8 +8 -8 -8 -8 -8 -8

That's sort of cool! Congrats!

I don't think there would be a concise expression for this table. I
mean, there is no two-variable polynomial which would describe this
table.

Proof: assume there is p(x,y) s.t. the i-th element of the j-th row can
be obtained as p(i,j). Then fix j=1. Thus you get a polynomial q(x) as
well. Take the delta of it: d(x) = q(x) - q(x-1). That's a polynomial
again, and we know its values at positive integers: d(n) = 1 if n is
even and d(n) = -1 if n is odd. As you'd write in ruby, d(n).abs <= 1.
But that's a contradiction, as polynomials tend to either plus or minus
infinity at infinity.

So the best formalization is writing an algorithm which calculates table
values based on the description Edgardo gave.

Csaba

Martin DeMello
Guest
Posts: n/a

 04-11-2005
Zach Dennis <(E-Mail Removed)> wrote:
>
> 1 2 3 4 5 6 (etc......)
> ----------------------------------------------------
> a 1 | 0 3 2 5 4 7
> b 2 | 3 0 1 6 7 4
> c 3 | 2 1 0 7 6 5
> d 4 | 5 6 7 0 1 2
> e 5 | 4 7 6 1 0 3
> f 6 | 7 4 5 2 3 0
> g 7 | 6 5 4 3 2 1
> h 8 | 9 10 11 12 13 14
> i 9 | 8 11 10 13 12 15
> j 10 | 11 8 9 14 15 12
> k 11 | 10 9 8 15 14 13
> l 12 | 13 14 15 8 9 10
> m 13 | 12 15 14 9 8 11
> n 14 | 15 12 13 10 11 8
> o 15 | 14 13 12 11 10 9
> p 16 | 17 18 19 20 21 22
> q 17 | 16 19 18 21 20 23
> r 18 | 19 16 17 22 23 20
> s 19 | 18 17 16 23 22 21
> t 20 | 21 22 23 16 17 18
> u 21 | 20 23 22 17 16 19
> v 22 | 23 20 21 18 19 16
> w 23 | 22 21 20 19 18 17
> x 24 | 25 26 27 28 29 30
> y 25 | 24 27 26 29 28 31
> z 26 | 27 24 25 30 31 28

Gotit! Try letter xor column

martin