This problem is also described in detail in Chapter 1.3
http://en.wikipedia.org/wiki/Concrete_Mathematics
Keeping long story short:
If we have N = 2m+l persons than the number of person who survives is 2 * l+ 1 (first person in line has number 1).It can be proved using induction.
But I decided to create small C++ program to verify it and prove the formula using "programmer proof".
Header:
http://code.google.com/p/javahotel/source/browse/trunk/examples/flavius/runflav.hextern int CountFlavByExclusion(int no);First method counts "Josephus" number using direct algorithm, the second is using formula.
extern int CountFlavByFormula(int no)
Implementation (STL std::vector is used) :
http://code.google.com/p/javahotel/source/browse/trunk/examples/flavius/runflav.cpp
Than finally test it ! (boost test framework is used)
http://code.google.com/p/javahotel/source/browse/trunk/examples/flavius/projflav.cppResult : !
Running 3 test cases...
*** No errors detected
Press Enter to continue!
Brak komentarzy:
Prześlij komentarz