Skip to content

Instantly share code, notes, and snippets.

@meriy100
Created February 2, 2018 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meriy100/ed21852dad87e81cac2f0b323b30cc05 to your computer and use it in GitHub Desktop.
Save meriy100/ed21852dad87e81cac2f0b323b30cc05 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int josephus ( int n ) {
int result ;
if ( ( n == 1 ) || ( n < 1 ) ) {
return 1 ;
}
else if ( n % 2 == 0 ) {
result = 2 * josephus ( n / 2 ) - 1 ;
return result ;
}
else {
result = 2 * josephus ( n / 2 ) + 1 ;
return result ;
}
}
int main ( void ) {
int n ;
while ( ( scanf ( "%d" , & n ) ) == 1 ) {
printf ( "%d\n" , josephus ( n ) ) ;
}
return ( 0 ) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment