mathematics – Grey Panthers Savannah https://grey-panther.net Just another WordPress site Sun, 06 Mar 2011 08:39:00 +0000 en-US hourly 1 https://wordpress.org/?v=7.0.4 206299117 Doing some estimations https://grey-panther.net/2011/03/doing-some-estimations.html https://grey-panther.net/2011/03/doing-some-estimations.html#respond Sun, 06 Mar 2011 08:39:00 +0000 https://grey-panther.net/?p=75 This is again one of those topics which I like to rant about, so I give you the short version: when you see a number, question it! Most of the numbers thrown at us in different media can be disproven quite easily and it is our responsibility as people not to just repeat whatever we’ve heard, but rather stop and think a little about it (of course I’m not immune to this myself, since I’ve just fallen into this trap when reading the “Contemplating Financial Trading At Picosecond Resolution” on Slashdot, only to see the very insightful comment: light travels 3mm in a picosecond – yes I’ve done the math – so this article is pure BS).

Offtopic: why do sayings in different languages have so much in common? For example we have the “beating the dead horse” expression in English and in Hungarian we would say somebody is talking about is “horse made of branch” (vesszoparipa). Ain’t it interesting?

Getting back to my rant :-). I’ve seen an article recently about a local (Romanian) affiliate program: eMAG Profitshare 2010. I applaud them for their openness and it also gives us the possibility to do a quick calculation. They say that they’ve given out 463 000 RON (~109 905 EUR / 153 499 USD) to 8690 sites.

Does it sound like a lot? Yes. Is it a lot for each individual site? Unlikely. Lets do a quick math: assuming that each site gets the same share (a very simplistic assumption) we have: 109 905 EUR / 8690 site = ~13 EUR per site / per year (these are yearly figures for 2010) so around 1 EUR (!) per site per month (!).

Ok, so be more real. You have a big fanbase, so you should be in the top sites as revenue. Lets consider a binomial distribution of the sites and do a little chart with Google Docs:

chart_1

What you see here is the revenue per month for a site in a certain category (categories are from 1 to 10, 1 being the lowest traffic one and 10 the highest traffic one). The number is in EUR. The conclusion: this business model is a very poor revenue source for the individuals participating, but probably a very good marketing avenue for companies (I assume that the cost for companies is around the same as doing a ad campaign, but the returns must be much better – not to mention the google juice they must be getting from this referrals!).

PS: in the name of transparency, you can see the sheet I used for calculation here.

]]>
https://grey-panther.net/2011/03/doing-some-estimations.html/feed 0 75
Solving mathematical puzzles with brute-force and Perl https://grey-panther.net/2010/03/solving-mathematical-puzzles-with-brute-force-and-perl.html https://grey-panther.net/2010/03/solving-mathematical-puzzles-with-brute-force-and-perl.html#respond Fri, 26 Mar 2010 09:47:00 +0000 https://grey-panther.net/?p=111 After talking a lot about optimizations and selecting the right algorithm, here is a little brute-force code. This particular one gives the answer to the following puzzle from Richard Wiseman’s Blog (one well worth following BTW):

Can you make the number 24 with the number 5, 5, 5, and 1 (again, you cannot join the numbers together, have to use each number once and only once, and are only allowed to add, subtract, multiply or divide them)?

And here is my brute-force solution:


permute(0, [5, 5, 5, 1], []);

sub permute {
  my ($partial, $numbers, $solution) = @_;
  
  if (0 == scalar(@$numbers)) {
    print @$solution, "n" if (24 == $partial);
  }
  else {
    for my $num (@$numbers) {
      my $mynums = [];
      my $skipped = 0;
      for my $mynum (@$numbers) {
        if ($num == $mynum && !$skipped) {
          $skipped = 1;
        }
        else {
          push @$mynums, $mynum;
        }
      }
      
      for my $op (qw(- + * /)) {
        my $mypart = eval "$partial $op $num";
        my $mysol  = [@$solution, $op, $num];
        permute($mypart, $mynums, $mysol);
      }
    }
  }  
}

The output is not very elegant and contains a decent amount of garbage (because it considers that we have a hidden zero at the start – ie. 0*5…) and also a lot of repetition (because it doesn’t take into account that 5 5 5 1 is the same as 5 5 5 1 with the first two numbers interchanged), but in the end it gives the correct answer:


... fake answers because it starts with 0 ...
*5+5*5-1
*5+5*5-1
/5+5*5-1
/5+5*5-1
*5+5*5-1
*5+5*5-1
/5+5*5-1
/5+5*5-1
*5+5*5-1
*5+5*5-1
/5+5*5-1
/5+5*5-1
... duplicate answers because of the order ...
-1/5+5*5
-1/5+5*5
-1/5+5*5
-1/5+5*5
-1/5+5*5
-1/5+5*5

Also the correct interpretation of the output is to consider that each operation has a pair of parentheses around them and not reading it according to the usual mathematical rules. Having this in mind the solution becomes:

((-1/5)+5)*5 = 4.8 * 5 = 24

Brute-force FTW 🙂

]]>
https://grey-panther.net/2010/03/solving-mathematical-puzzles-with-brute-force-and-perl.html/feed 0 111
Computing the last digit of b^e efficiently https://grey-panther.net/2010/03/computing-the-last-digit-of-be-efficiently.html https://grey-panther.net/2010/03/computing-the-last-digit-of-be-efficiently.html#comments Mon, 15 Mar 2010 11:20:00 +0000 https://grey-panther.net/?p=122 Geek PSA: Yesterday was PI day (3.14, get it?). Lets celebrate with this spiked math comic:

Last week I saw the following problem which peaked my interest:

Compute the last (decimal) digit of 2 raised to the power e where e might be very large.

We assume that we are talking about positive, integer exponents here. The key observation here is that the last digit form a cycle:

...2 * 2 = ...4
...4 * 2 = ...8
...8 * 2 = ...6
...6 * 2 = ...2

So you can compute the last digit by calculating e MOD 4, which gives us the position in the cycle. Next, I wondered if all the digits have this cycling going on, so I searched around a little bit and found this page which gives the cycle for all ten digits. Now you can compute the last digit of the formula b^e with the following algorithm:

  • Take the last digit of b
  • compute e MOD (length of cycle for last digit of b)
  • return the given element of the cycle

This is very nice, since we can operate on arbitrary sized b, but we still need to be able to perform the modulo operation on e, which might be inconvenient if e is large. Fortunately we can make the following observation:

  • The length of the cycle is either 1, 2 or 4
  • For x in [1,2,4] you have …ab MOD x = ab MOD x (ie you can take only the last two digits and compute the modulo) since 100 is a multiple of both 2 and 4 (meaning that a number in the form of …00 will always be divisible by both 2 and 4, hence it contributes nothing to the modulo operation)

So here is the final algorithm which works quickly even for very large values of b and e

  • Take the last digit of b
  • compute e MOD length_of_cycle by using the last two digits of e
  • return the given element of the cycle

Give it a try below if you have javascript enabled:

Math is fun :-).

Finally, I would like to leave you with the following question: is it possible to efficiently compute an arbitrary digit of b^e? My intuition is that there are cycles in all of them, however they might be quite long…

]]>
https://grey-panther.net/2010/03/computing-the-last-digit-of-be-efficiently.html/feed 3 122
Pi day and other ramblings https://grey-panther.net/2009/03/pi-day-and-other-ramblings.html https://grey-panther.net/2009/03/pi-day-and-other-ramblings.html#comments Sun, 15 Mar 2009 18:39:00 +0000 https://grey-panther.net/?p=358 2332789392_6376129e6c_bThe USA congress decided that March the 14th is national Pi day (from chuckchat.com). While I’m not a USA citizen and live nowhere near it, I still thought that the idea is cute. Math can be fun (even though the education systems gives the opposite impression frequently).

A fun fact which I was amazed by when I learned it many years ago: you can calculate Pi probabilistically. The basic idea is to set up an experiment where the probability of the outcome involves Pi. Then perform the experiment a number of times (the higher, the better, since you approximate the actual probability more closely), then go backwards (assume that the actual probability is the one obtained in the experiment and calculate Pi from it). This webpage demonstrates the concept with a Java applet: you put points randomly on a rectangle bounding a circle (technically, one quarter of the circle) and you ask the question: what is the probability of the point ending up inside versus outside of the circle?

The kids on the picture below do a similar thing. Here the question is: what is the probability for the stick frozen hot-dog landing such that it crosses a horizontal line? The math here is slightly more involved, and Pi comes in because you have to take into consideration the angle of the stick hot-dog:

3352182691_90b00a3d9f_o

Remaining at the mathematics / fun facts theme, here is a puzzle from abstrusegoose:

simple_puzzle2The site already contains the solution (and so does Google :-)), but it still might be interesting to have a go at it. My solution to the problem (in Perl) can be found in my SVN repo. With this occasion I found out that Math::Prime::XS segfaults, but fortunately the naive algorithms I’ve implemented found the solution quickly.

First picture taken from Mykl Roventine’s photostream with permission. The second picture is taken from Jose Kevo’s photostream with permission.

Update: changed stick to frozen hot-dog.

]]>
https://grey-panther.net/2009/03/pi-day-and-other-ramblings.html/feed 2 358
All natural numbers are special https://grey-panther.net/2006/11/all-natural-numbers-are-special.html https://grey-panther.net/2006/11/all-natural-numbers-are-special.html#respond Sun, 19 Nov 2006 14:38:00 +0000 https://grey-panther.net/?p=1015 From the department of too-geeky-joke-to-be-funny comes the following one:

All natural numbers are special:

  • 0 is special because it is the first natural number
  • 1 is special because it is the neutral element for multiplication
  • 2 is special because it is the first even number
  • 3 is special because it is the first odd prime number
  • lets suppose that there exists a non-empty set of non-special natural numbers
  • we can take the minimum of this set – but this number is special because it is the smallest non-special number
  • QED 🙂

(Original source – in Romanian)

]]>
https://grey-panther.net/2006/11/all-natural-numbers-are-special.html/feed 0 1015