Photosynthetically active radiation

From formulasearchengine
Revision as of 06:42, 4 February 2014 by en>Mikiemike (Yield Photon Flux: YPF function approx, table)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In computer science and mathematics, the Josephus Problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game.

There are people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.

The task is to choose the place in the initial circle so that you are the last one remaining and so survive.

History

The problem is named after Flavius Josephus, a Jewish historian living in the 1st century. According to Josephus' account of the siege of Yodfat, he and his 40 soldiers were trapped in a cave, the exit of which was blocked by Romans. They chose suicide over capture and decided that they would form a circle and start killing themselves using a step of three. Josephus states that by luck or possibly by the hand of God, he and another man remained the last and gave up to the Romans.

The reference comes from Book 3, Chapter 8, par 7 of Josephus' The Jewish War (writing of himself in the third person):

36 year-old Diving Instructor (Open water ) Vancamp from Kuujjuaq, spends time with pursuits for instance gardening, public listed property developers in singapore developers in singapore and cigar smoking. Of late took some time to go China Danxia.

Solution

In the following, n denotes the number of people in the initial circle, and k denotes the count for each step, that is, k1 people are skipped and the k-th is executed. The people in the circle are numbered from 1 to n.

k=2

We explicitly solve the problem when every 2nd person will be killed, i.e. k=2. (For the more general case k2, we outline a solution below.) We express the solution recursively. Let f(n) denote the position of the survivor when there are initially n people (and k=2). The first time around the circle, all of the even-numbered people die. The second time around the circle, the new 2nd person dies, then the new 4th person, etc.; it's as though there were no first time around the circle.

If the initial number of people was even, then the person in position x during the second time around the circle was originally in position 2x1 (for every choice of x). Let n=2j. The person at f(j) who will now survive was originally in position 2f(j)1. This gives us the recurrence

f(2j)=2f(j)1.

If the initial number of people was odd, then we think of person 1 as dying at the end of the first time around the circle. Again, during the second time around the circle, the new 2nd person dies, then the new 4th person, etc. In this case, the person in position x was originally in position 2x+1. This gives us the recurrence

f(2j+1)=2f(j)+1.

When we tabulate the values of n and f(n) we see a pattern:

n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
f(n) 1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1

This suggests that f(n) is an increasing odd sequence that restarts with f(n)=1 whenever the index n is a power of 2. Therefore, if we choose m and l so that n=2m+l and 0l<2m, then f(n)=2l+1. It is clear that values in the table satisfy this equation. Or we can think that after l people are dead there are only 2m people and we go to the 2l+1th person. He must be the survivor. So f(n)=2l+1. Below, we give a proof by induction.

Theorem: If n=2m+l and 0l<2m, then f(n)=2l+1.

Proof: We use strong induction on n. The base case n=1 is true. We consider separately the cases when n is even and when n is odd.

If n is even, then choose l1 and m1 such that n/2=2m1+l1 and 0l1<2m1. Note that l1=l/2. We have f(n)=2f(n/2)1=2((2l1)+1)1=2l+1, where the second equality follows from the induction hypothesis.

If n is odd, then choose l1 and m1 such that (n1)/2=2m1+l1 and 0l1<2m1. Note that l1=(l1)/2. We have f(n)=2f((n1)/2)+1=2((2l1)+1)+1=2l+1, where the second equality follows from the induction hypothesis. This completes the proof.

We can solve for l to get an explicit expression for f(n):

f(n)=2(n2log2(n))+1

The most elegant form of the answer involves the binary representation of size n: f(n) can be obtained by a one-bit left cyclic shift of n itself. If we represent n in binary as n=1b1b2b3bm, then the solution is given by f(n)=b1b2b3bm1. The proof of this follows from the representation of n as 2m+l or from the above expression for f(n).

Implementation: A simple python function can be:

  def josephus_2( n ):
    from math import log
    return 2*(n - 2**(int(log(n,2))))+1

The general case

The easiest way to solve this problem in the general case is to use dynamic programming by performing the first step and then using the solution of the remaining problem. When the index starts from one, then the person at s shifts from the first person is in position ((s1)modn)+1, where n is the total number of persons. Let f(n,k) denote the position of the survivor. After the k-th person is killed, we're left with a circle of n1, and we start the next count with the person whose number in the original problem was (kmodn)+1. The position of the survivor in the remaining circle would be f(n1,k) if we start counting at 1; shifting this to account for the fact that we're starting at (kmodn)+1 yields the recurrence

f(n,k)=((f(n1,k)+k1)modn)+1, with f(1,k)=1,

which takes the simpler form

g(n,k)=(g(n1,k)+k)modn, with g(1,k)=0

if we number the positions from 0 to n1 instead.

This approach has running time O(n), but for small k and large n there is another approach. The second approach also uses dynamic programming but has running time O(klogn). It is based on considering killing k-th, 2k-th, ..., (n/kk)-th people as one step, then changing the numbering.

Implementation: A simple python function can be (but might be slow/fail for large n and small k):

  def josephus(n, k):
    if n ==1:
      return 1
    else:
      return ((josephus(n-1,k)+k-1) % n)+1

Alternatively a simple loop can also be run like:

  def josephus(n, k):
    r = 0
    i = 1
    while i <= n:
      r = (r + k) % i;
      i+= 1
    return r+1

A dynamic programming solution in Haskell:

  josephus n k = josephus' [1..n] k where
      josephus' xs k
          | length xs == 1 = head xs
          | otherwise      = josephus' (killnext k xs) k where
          killnext k xs = take (length xs - 1) (drop k (cycle xs))

A somewhat more elaborate version in Java:

  int josephus(int n, int k) {
        return josephus(n, k, 1);
  }
  int josephus(int n, int k, int startingPoint) {
      if(n == 1)
          return 1;
      int newSp = (startingPoint + k - 2) % n + 1;
  
      int survivor = josephus(n - 1, k, newSp);
      if (survivor < newSp) {
          return survivor;
      } else
          return survivor + 1;
  }

Variants and generalizations

According to Concrete Mathematics, section 1.3, Josephus had an accomplice; the problem was then to find the places of the two last remaining survivors (whose conspiracy would ensure their survival).

Extended Josephus problem

Problem definition: There are n persons, numbered 1 to n, around a circle. We eliminate second of every two remaining persons until one person remains. Given the n, determine the number of xth person who is eliminated.[1]

Notes

43 year old Petroleum Engineer Harry from Deep River, usually spends time with hobbies and interests like renting movies, property developers in singapore new condominium and vehicle racing. Constantly enjoys going to destinations like Camino Real de Tierra Adentro.

References

  • 20 year-old Real Estate Agent Rusty from Saint-Paul, has hobbies and interests which includes monopoly, property developers in singapore and poker. Will soon undertake a contiki trip that may include going to the Lower Valley of the Omo.

    My blog: http://www.primaboinca.com/view_profile.php?userid=5889534

External links