Example of the Joseph Ring Algorithm Based on Recursive Implementation in PHP, Recursive Algorithm
This example describes PHP's Joseph Ring Algorithm Based on recursion. We will share this with you for your reference. The details are as follows:
Joseph's ring problem:39 Jews hid in a cave with Joseph and his friends, and 39 Jews decided to die rather than be captured by the enemy. Therefore, the method of suicide was decided. 41 people were arranged in a circle, and the number of reports started from 1st. Each report to 3rd people had to commit suicide. Next, report the number again until everyone commits suicide. However, Joseph and his friends didn't want to follow suit. He asked his friends to pretend to follow suit first. He arranged his friends and himself in 16th and 31st positions, so he escaped the death game.
<? Php $ num = 41; $ step = 3; function joseph ($ arr, $ step, $ start, $ specify vors) {foreach ($ arr as $ k => $ v) {if ($ start % $ step = 0) {unset ($ arr [$ k]); $ start = 1 ;}else {$ start ++ ;}} if (count ($ arr)> $ variable vors) return joseph ($ arr, $ step, $ start, $ variable vors); else return $ arr ;}$ I = 0; $ arr = []; while ($ I ++ <$ num) {$ arr [] = $ I;} $ arr = joseph ($ arr, 3, 1, 2); print_r ($ arr );
Execution result:
Array ([15] => 16 [30] => 31)