You are on page 1of 6

312317205096

1.JOSEPHUS PROBLEM
INTRODUCTION:
In computer science and mathematics , the Josephus problem (or Josephus permutation) is
a theoretical problem related to a certain counting-out game. People are standing in a circle
waiting to be executed. Counting begins at a specified point in the circle and proceeds around the
circle in a specified direction. After a specified number of people are skipped, the next person is
executed. The procedure is repeated with the remaining people, starting with the next person, going
in the same direction and skipping the same number of people, until only one person remains, and is
freed.
The problem — given the number of people, starting point, direction, and number to be skipped — is
to choose the position in the initial circle to avoid execution.

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 by Roman soldiers. They chose suicide over capture, and settled on a serial method of
committing suicide by drawing lots. Josephus states that by luck or possibly by the hand of God, he
and another man remained until the end and surrendered to the Romans rather than killing
themselves.

SOLUTION:
In the following, n denotes the number of people in the initial circle, and k denotes the count for each
step, that is,k-1 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 second person will be killed, i.e. k=2. (For the more
general case k\neq 2, 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.

Even:

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 . The person at f(j)}f(j) who will now survive was originally in
position 2f(j)-1}2f(j)-1. This gives us the recurrence

Odd:
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
312317205096

new 4th person, etc. In this case, the person in position x was originally in position 2x+1}2x+1. This
gives us the recurrence

f(2j+1)=2f(j)+1\f(2j+1)=2f(j)+1.
When we tabulate the values of {\displaystyle n}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)}f(n)
1 1 3 1 3 5 7 1 3 5 7
9 11 13 15 1

This suggests that f(n)}f(n) is an increasing odd sequence that restarts with f(n)=1}f(n)=1 whenever
the index n is a power of 2. Therefore, if we choose m and l so that n=2^{m}+l}n=2^{m}+l and 0\leq
l<2^{m}}0\leq l<2^{m}, then f(n)=2\cdot l+1}f(n)=2\cdot l+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 2^{m}}2^{m} people and we
go to the 2l+1th person. He must be the survivor. So f(n)=2l+1.f(n)=2l+1. Below, we give a proof by
induction.

Theorem: If n=2^{m}+l}n=2^{m}+l and 0\leq l<2^{m}}0\leq l<2^{m}, then f(n)=2l+1}f(n)=2l+1.

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

If n is even, then choose l_{1} and m_{1} such that n/2=2^{m_{1}}+l_{1}}n/2=2^{{m_{1}}}+l_{1} and
0\leq l_{1}<2^{m_{1}}}0\leq l_{1}<2^{{m_{1}}}. Note that l=l/2}l_{1}=l/2. We have f(n)=2f(n/2)-
1=2((2l_{1})+1)-1=2l+1}f(n)=2f(n/2)-1=2((2l_{1})+1)-1=2l+1, where the second equality follows from
the induction hypothesis.

If n is odd, then choose l_{1} and m_{1}}m_{1} such that


(n-1)/2=2^{m_{1}}+l_{1}}(n-1)/2=2^{{m_{1}}}+l_{1} and 0\leq l_{1}<2^{m_{1}}}0\leq l_{1}<2^{{m_{1}}}.
Note that l=(l-1)/2}l_{1}=(l-1)/2. We have f(n)=2f((n-1)/2)+1=2((2l_{1})+1)+1=2l+1}f(n)=2f((n-
1)/2)+1=2((2l_{1})+1)+1=2l+1, where the second equality follows from the induction hypothesis. This
completes the proof.

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

f(n)=2(n-2^{\log _{2}(n)\rfloor })+1}f(n)=2(n-2^{{ \log _{2}(n)\rfloor }})+1


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

Implementation: If n denotes the number of people, the safe position is given by the function
f(n)=2l+1}f(n)=2l+1 ,where n=2^{m}+l}n=2^{m}+l and 0\leq l<2^{m}}0\leq l<2^{m}.

Now if we represent the number in binary format, the first bit denotes 2^{m}}2^{m} and remaining
bits will denote Il. For example, when n=41, its binary representation is

n=101001

2m = 1 0 0 0 0 0

l = 0 1 0 0 1.

This is the solution to the problem.

CONCLUSION:
This means that Josephus had to be standing at postion 19 in a circle of 41 to
escape from persecution. However in the real scenario,Josephus convinced another fellow Jew
and they both surrendered themselves t o the Germans.

TOPIC-2:JAVASCRIPT
INTRODUCTION:
JavaScript often abbreviated as JS, is a programming language that conforms to the
ECMAScript specification.[7] JavaScript is high-level, often just-in-time compiled, and multi-
paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-
class functions.

Alongside HTML and CSS, JavaScript is one of the core technologies of the World Wide Web.[8]
JavaScript enables interactive web pages and is an essential part of web applications. The vast
majority of websites use it for client-side page behavior,[9] and all major web browsers have a
dedicated JavaScript engine to execute it.

As a multi-paradigm language, JavaScript supports event-driven, functional, and imperative


programming styles. It has application programming interfaces (APIs) for working with text, dates,
regular expressions, standard data structures, and the Document Object Model (DOM). However,
the language itself does not include any input/output (I/O), such as networking, storage, or graphics
facilities, as the host environment (usually a web browser) provides those APIs.

Originally used only in web browsers, JavaScript engines are also now embedded in server-side
website deployments and non-browser applications.
312317205096

DATA TYPES IN JAVASCRIPT:


Data types basically specify what kind of data can be stored and manipulated within a program.

There are six basic data types in JavaScript which can be divided into three main categories:
primitive (or primary), composite (or reference) , and special data types. String, Number, and
Boolean are primitive data types. Object, Array, and Function (which are all types of objects)
are composite data types. Whereas Undefined and Null are special data types.

The String Data Type


The string data type is used to represent textual data (i.e. sequences of
characters). Strings are created using single or double quotes surrounding one or
more characters.

var a = 'Hi there!';

The Number Data Type


The number data type is used to represent positive or negative numbers with or
without decimal place, or numbers written using exponential notation e.g. 1.5e-4
(equivalent to 1.5x10 -4).

var a = 25; // integer var b = 80.5; // floating-point number

var c = 4.25e+6; // exponential notation, same as 4.25e6 or 4250000

var d = 4.25e-6; // exponential n otation, same as 0.00000425

The Boolean Data Type


The Boolean data type can hold only two values: true or false. It is

typically used to store values like yes (true ) or no (false ) , on


312317205096

(true ) or off (false ) , var isReading = true; // yes, I'm


reading var isSleeping = false; // no, I'm not sleeping

The Undefined Data Type


The undefined data type can only have one value-the special value undefined.
If a variable has been declared, but has not been assigned a value, has the value
undefined.

var a; var b =

"Hello World!"

alert(a) // Output: undefined

alert(b) // Output: Hello World!

The Null Data Type


This is another special data type that can have only one value-the null value. A
null value means that there is no value. It is not equivalent to an empty string
("" ) or 0, it is simply nothing.

A variable can be explicitly emptied of its current contents by assigning it the


null value.

var a = null; alert(a); // Output: null

The Object Data Type


The object is a complex data type that allows you to store collections of data.
312317205096

An object contains properties, defined as a key-value pair. A property key (name)


is always a string, but the value can be any data type, like strings, numbers,
booleans, or complex data types like arrays, function and other objects.

var emptyObject = {}; var person = {"name": "Clark",

"surname": "Kent", "age": "36"};

The Array Data Type


An array is a type of object used for storing multiple values in single variable.
Each value (also called an element) in an array has a numeric position, known as
its index, and it may contain data of any data type-numbers, strings, booleans,
functions, objects, and even other arrays. The array index starts from 0, so that
the first array element is arr[0] not arr[1]. var colors = ["Red",
"Yellow", "Green", "Orange"];

The Function Data Type


The function is callable object that executes a block of code. Since functions are
objects, so it is possible to assign them to variables, as shown in the example
below:

var greeting = function(){

return "Hello World!";

These are all the Data Types in Javascript.

You might also like