Showing posts with label First Success. Show all posts
Showing posts with label First Success. Show all posts

Friday, August 14, 2009

CEIL(x)

As promised, I have returned to reveal the mystical secrets of the math universe. Yes, step right up, step right up!

So, now that we have the FLOOR(x) function, now we are going to build on that to create the CEIL(x) function.

Refresher:
FLOOR(x) = x- (arccos(cos(x*2pi))/2pi)*IS_POS - (1-arccos(cos(x*2pi))/2pi)*(NOT(IS_POS))

IS_POS was our switch function that equaled 1 when sin(x*2pi) was positive or 0. Using the fact that 0^0 = 1 and 0^(any other positive number) = 0, it went like this:
IS_POS = 0^abs(sin(x*2pi)-abs(sin(x*2pi)))

And the NOT(x) function there could be implemented a few ways. In the floor function post, I again used the properies of 0 raised to flip the IS_POS switch like this:

IS_POS = 1
0^abs(IS_POS) = 0^1 = 0

IS_POS = 0
0^abs(IS_POS) = 0^0 = 1

Perdy neat!

But this time, and for no real particular reason except to be explorative, we'll redefine the NOT(x) function.

Using the constraint of IS_POS (it can only be 0 or 1), we can make the function:

IS_POS = 1
NOT(IS_POS) = 1 - IS_POS1 = 1-1 = 0

IS_POS = 0
NOT(IS_POS) = 1 - IS_POS1 = 1-0 = 1

This is an old computer programming trick.

Anyway, back from the tangent to the task at hand; Developing a working CEIL(x) function! I just wanted to get everybody back on the same page.

So looking at FLOOR(x) again:
FLOOR(x) = x- (arccos(cos(x*2pi))/2pi)*IS_POS - (1-arccos(cos(x*2pi))/2pi)*(NOT(IS_POS))

we have these switches on two very similar looking chunks of the equations-
First:
arccos(cos(x*2pi))/2pi

Second:
1-arccos(cos(x*2pi))/2pi

Well, the first is "active" (multiplied by 1) when the IS_POS function is true and "deactivated" (multiplied by 0) when the IS_POS function is returning 0. Conversely the second is activated and deactivated oppositely, thanks to the NOT(x) function.

The reason for this is (explained by the following exaples of flooring 3.r were r is the decimal or fractional part of the number):
arccos(cos(3.00*2pi))/2pi = 0.00
arccos(cos(3.25*2pi))/2pi = 0.25
arccos(cos(3.50*2pi))/2pi = 0.50

so our neat little equation there returns just the fractional part of the number for us but, because the cos function is cyclical, after 3.5 up to 4.0, the result reverses:
arccos(cos(3.60*2pi))/2pi = 0.40, but +1-0.40 = 0.60
arccos(cos(3.75*2pi))/2pi = 0.25, but +1-0.25 = 0.75
arccos(cos(3.90*2pi))/2p) = 0.10, but +1-0.10 = 0.90

So, you see, for 3.0 to 3.5, we want to just straight subtract off the result, but once we are past 3.5 but less than 4 (when the IS_POS is false), we need to subtract off the difference between 1 to make the floor function work.

Ok, so we are all caught up on how it works, so let's use these blocks to make the CEIL function!

CEIL(x) = x+what?

Well, let's see what CEIL(x) should return (using 3.r again):
CEIL(3.00) = 3.00
CEIL(3.25) = 4.00
CEIL(3.50) = 4.00
CEIL(3.75) = 4.00
CEIL(4.00) = 4.00

So now, we need to add, but let's lookj at what we need to add:
CEIL(3.00) = 3.00, add 0.00
CEIL(3.25) = 4.00, add 0.75
CEIL(3.50) = 4.00, add 0.50
CEIL(3.75) = 4.00, add 0.25
CEIL(4.00) = 4.00, add 0.00

Well, it looks like we are adding 1-r (remember that r was the fractional part) so:
CEIL(x) = x+(1-(arccos(cos(x*2pi))/2pi))

Let's test it and make sure:
CEIL(3.25) = 3.25+(1-(arccos(cos(3.25*2pi))/2pi)) = 3.25+0.75 = 4.0, Good
CEIL(3.50) = 3.50+(1-(arccos(cos(3.50*2pi))/2pi)) = 3.50+0.50 = 4.0, Good
CEIL(3.75) = 3.75+(1-(arccos(cos(3.75*2pi))/2pi)) = 3.75+0.75 = 4.5, oops!
CEIL(4.00) = 4.00+(1-(arccos(cos(5.00*2pi))/2pi)) = 4.00+1.00 = 5.0, oops!

So what went wrong? Well, 2 things:
1) Integers (like 3.0 and 4.0) should stay as they are, not have 1 added to them
2) In the CEIL(3.75) example, the old cos function being cyclical thing got us again.

so, for 3.5 to 4.0 and on the integers(3.00 and 4.00 for example) we want to add just the fractional part, not the 1-fractional. This is very much the opposite of what we were seing before. Ofcourse that makes sence in that the FLOOR(x) and the CEIL(x) are opposites.
So let's go crazy and create an IS_NEG function that is equal to 1 when sin(x*2pi) is negative or 0!

IS_NEG = 0^abs(-sin(x*2pi)-abs(sin(x*2pi)))

What's the difference from IS_POS? I multiplied the first sin * -1. Now, if sin(x*2pi) is positive, then it becomes a negative and then subtracting off the abs value will not be 0, and because 0 raised to anything but 0 equals 0, IS_NEG will be False ( or 0).

This means that IS_NEG will equal 1 when sin(x*2pi) equals 0 or is negative. Perfect.

So let's work the rest of the peices out:

CEIL(x) = x+ (arccos(cos(x*2pi))/2pi)*IS_NEG + (1-arccos(cos(x*2pi))/2pi)*(NOT(IS_NEG))


And there we go. A working CEIL(x) function.

Wednesday, August 12, 2009

FLOOR(X) - And so the saga continues...

So, the ultimate answer to the ultimate question of life, the universe and everything is...

Oops, wrong story.

But in the last episode, I was looking for a signal that would tell me if I was greater that half way around the circle, so I could signal the arccos function to keep increasing, and that answer is sin.

Sin, being the y axis vector, is positive while traversing the circle's circumference counterclockwise for half the journey, and then becomes negative. That is exactly the indicator I need, but how can i make it work for me?

Well, there is an odd, and little known, fact about exponents, that goes something like this:

x^0=1 (any number raised to the 0th power is 1)
0^x = 0 (0 raised to any power is zero)

putting these two equations at conflict. To solve this, it was determined that exponents actually take the form:



that says, recursively, start with 1, and then multiply x to it, y times. Here's an example:

5^3 = 1 * 5 *5 * 5 = 125
5^2 = 1 * 5 * 5 = 25
5^1 = 1 * 5 = 5
5^0 = 1

or for 0:

0^3 = 1 * 0 * 0 * 0 = 0
0^2 = 1 * 0 * 0 = 0
0^1 = 1 * 0 = 0
0^0 = 1

Very interesting, but more importantly, this acts like a built in mathematical switch! In order to use this switch, we write an equation for the power applied to 0, and we know that the total result will be 0, until the equation is equal to 0, thus making 0^0 = 1.

Let me use an example to clarify:

Say I revisited my original goal to find an equation that would indicate if a number is an integer or not:

Well, thinking about the trig functions and the unit circle,

sin(n*pi) = 0

n representing only integers. Non integers will return a number other than 0. Again, an example to clarify:

sin( 0.0 * pi) = 0
sin( 0.5 * pi) = 1
sin( 1.0 * pi) = 0
sin( 1.5 * pi) = -1
sin( 2.0 * pi) = 0

this seems to meet our conditions for this switch. We wanted 0^0 only when there is an integer, so a true integer detection equation would be:

0^sin(x*pi)

well, almost. You can't have a negative power (remember, roots are fractional powers, not negative) with 0^x or you would get div-by-zero, so an easy solution to this is the absolute value function, which basically removes the negative sign from all negative numbers. So, we really need:

0^abs(sin(x*pi))

Viola!

So you get the jist of how this switch thingy-ma-bobber works, right? So back to the FLOOR(X) function. I currently have:

FLOOR(x) = x - (arccos(cos(2pi*x))/(2pi))

which works as long as the fractional part of x is 1/2 or less. So, using my switch and knowing the sin funxtion becomes negative when the fractional part of x is greater that 1/2, i can use a switch like:

0^abs(sin(x*2pi)-abs(sin(x*2pi)))

to tell me if sin(x*2pi) >= 0.

This works because:

arccos(cos(3.2*2pi))/2pi = 0.2 which requires us to subtract this result from the 3.2 to floor it.
arccos(cos(3.5*2pi))/2pi = 0.5 which requires us to subtract this result from the 3.5 to floor it.

but:

arccos(cos(3.6*2pi))/2pi = 0.4
arccos(cos(3.7*2pi))/2pi = 0.3
arccos(cos(3.8*2pi))/2pi = 0.2
arccos(cos(3.9*2pi))/2pi = 0.1

we now have to subtract 1-this result from the number we entered to floor it:

1-arccos(cos(3.6*2pi))/2pi = 0.6 and 3.6 - 0.6 = 3
1-arccos(cos(3.7*2pi))/2pi = 0.7 and 3.7 - 0.7 = 3
1-arccos(cos(3.8*2pi))/2pi = 0.8 and 3.8 - 0.8 = 3
1-arccos(cos(3.9*2pi))/2pi = 0.9 and 3.9 - 0.9 = 3

This new development is why we need the mathematical switch; without it, we have a piecewise function that isn't subject to all of the normal algebreaic motions. We can't reduce in a more complex equation, or apply commutative properties, etc. With the switch in place, however, it's game on.

So now we need the switch implimented:

x-arccos(cos(x*2pi))/2pi or x+1-arccos(cos(x*2pi))/2pi

to make it readable, I'm going to substitute the label IS_POS for the switch:

IS_POS = 0^abs(sin(x*2pi)-abs(sin(x*2pi)))

so we getsomething roughly like:

x- (arccos(cos(x*2pi))/2pi)*IS_POS - (1-arccos(cos(x*2pi))/2pi)*(NOT(IS_POS))

Ok, ok. I slipped one in there on ya. The not function basically says if IS_POS is 0, make it a 1, otherwise, make it a 0. Hmm, kinda like a switch...

NOT(a) = 0^abs(a)

so you see, the equation can be written:

x- (arccos(cos(x*2pi))/2pi)*IS_POS - (1-arccos(cos(x*2pi))/2pi)*0^abs(IS_POS)

but NOT is more human readable.

Enough for now, in the next post, I will make the CEIL(x) function work from these same blocks and then later I will write up the various MOD(x,y) functions.

FLOOR(x)

Monday, just before I left work, an old problem that had been swirling around in my head decided to surface. Mal and i worked on the RSA challenges (trying to factor really large numbers that are the product of two primes).

So, we found tons of solutions that would get us the answers quickly if we could put our equations into a non-recursive form. Unfortunately, they all used modulo or floor functions, which are conceptual, piecewise functions.

So, back to Monday; As I was packing up to go home, the thought that had occurred to me several times came to the front of my brain again, Integers are periodic. Numbers can be represented in many ways, but fractions make this Integers very clear.

Here is an example:
1.333333333333333(...) is a hard number to write. It could literally take for ever to write it out completely without using fractions. As a fraction though:

1+1/3 is all you need.

so I can talk about parts of it, I will use the following generic format for fractions:

q+(n/d)

where:
q is the "whole" number or the biggest possible number than n/d will allow
n is the numerator and has only the left overs (or remainder)
d is the denominator

What I mean by all of that is, say you had:

0+11/3

Then you are not really in the right form. You would want to simplify the (n/d) or fractional part as much as possible:

3+(2/3)

Is the best answer. But I do digress to my real point, that integers are periodic, or, as the thought actually came to me, cyclical. Integers are special numbers whose fractional part (n/d) is equal to 0.

If integers are cyclical, and the only mathematically cyclical thing I know of is trig functions (specifically sin and cos functions), then maybe I can use them.

I thought, I need to write an equation that is true only when I have hit some point on the unit circle, as i traverse the circumference. So I started looking at the circle and noticed the same thing probably a billion other people have, cos starts at 1 and goes to -1. There had to be something there, so i dropped my victim number into the cos function (with 2pi) and that didn't help me to much.

What did help me was remembering that the cos function always returned a number 1 to -1, no matter how many times you went around the circle. It, in effect, chopped of the whole number (the "q" in my form above) and left a wierd number, that was consistent base on my fractional part, but didn't directly match or seem to directly correlate.

Well, duh! cos is the x axis vector for where you are on the circumference of the unit circle. drop that result back into arccos and it should return how far around the circle my fractional part has taken me.

For instance:

cos(2pi*3.25) = 0
arccos(0) = pi/2

Well, that's not exactly returning just the fractional part of 3.25 (3+1/4) or the 0.25 (0+1.4), but that's because it's scaled by 2pi!

(pi/2)/(2pi) = 0.25

So, now, what if I take my 3.25 - (arccos(cos(2pi*3.25))/(2pi))?

3.25 - (arccos(cos(2pi*3.25))/(2pi)) = 3

Good stuff. I'm well on my way to being able for the equation to tell me if it is an integer or... Wait a second. Didn't that just separate the fractional part from the whole part? Did I just FLOOR that with a real equation? Friggin' wholly grails!

Upon testing there turns out to be a problem though. It works, returning q from q+(0/2) to q+(1/2), but then it sudenly breaks and ramps with a slope of 2 from ther on up to q+(2/2) (or q+1).

Now the problem with this solution emerges. cos is cyclical and get's to -1 half way around the circle, but then starts increasing back towards 1 there after. The arccos function can't tell the difference between the the -0.9 just before we get half way around the circle or just after. The slope of 2 is due to the fact that the rate in change in the number we are testing (3.4, 3.5, 3.6, 3.7, etc) is the same rate of change that is coming out of the arccos function, thusly doubling.

I'm sure I'm rambling and not making a ton of sense, but the important thing that occured to me was that, if I had some sort of indicator that I could use to tell the arccos function to just keep going up, it would work.

Now a few hours after I should have left work, I had developed some imperfect solutions, but the answer was in my grasp.

And with that I leave you with the thrilling cliffhanger of how I developed working equations of the FLOOR(x), CEIL(x), MOD(x,n) functions and much, much more! Untill next time, be sure to drink your Ovaltine!