From 042a4b0b79a0d1b9276a64c416991f4a0a377982 Mon Sep 17 00:00:00 2001 From: garvit088 Date: Mon, 17 Oct 2022 20:25:21 +0530 Subject: [PATCH 1/3] Update fibonacci.py --- algorithms/math/fibonacci.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/algorithms/math/fibonacci.py b/algorithms/math/fibonacci.py index 4420883..2324c69 100644 --- a/algorithms/math/fibonacci.py +++ b/algorithms/math/fibonacci.py @@ -1,11 +1,24 @@ -def fibonacciUn(Un_2,Un_1,n): +''' +In mathematics, the Fibonacci numbers, commonly denoted Fn , form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. Like for example first two terms of a sequence are 0 and 1. Now its fibonacci sequence will be as follows. + +0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144. +''' + +def fibonacciUn(Un_2,Un_1,n: int): + ''' + This function gives two more terms + of the fibonacci sequence after + given two terms. + ''' if(n<1): return Un_2 if (n==1): return Un_1 for i in range(n): fib=Un_1+Un_2 + print(fib) Un_2=Un_1 Un_1=fib return Un_1 -print(fibonacciUn(10,15,2)) \ No newline at end of file + +fibonacciUn(10,15,2) \ No newline at end of file From 32ed15a98a5c1237b26441bfdcae1a94435f1cc9 Mon Sep 17 00:00:00 2001 From: garvit088 Date: Mon, 17 Oct 2022 20:51:29 +0530 Subject: [PATCH 2/3] Create josephus_circle.py --- algorithms/linkedlist/josephus_circle.py | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 algorithms/linkedlist/josephus_circle.py diff --git a/algorithms/linkedlist/josephus_circle.py b/algorithms/linkedlist/josephus_circle.py new file mode 100644 index 0000000..0bf308b --- /dev/null +++ b/algorithms/linkedlist/josephus_circle.py @@ -0,0 +1,56 @@ +''' +Josephus Circle: +Let there be N 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. +''' +class node: + def __init__(self,data): + self.data=data + self.next=None + +class circle: + def __init__(self): + self.head=None + + def insert(self,data): + new=node(data) + new.next=self.head + temp=self.head + if self.head==None: + new.next=new + self.head=new + else: + while temp.next!=self.head: + temp=temp.next + temp.next=new + + def print_list(self): + temp=self.head + while temp: + print(temp.data) + temp=temp.next + if temp==self.head: + break + + def eliminate(self,n,k): + ''' + This function eliminates the kth number + from the list + ''' + temp=self.head + for _ in range(n-1): + for _ in range(k): + temp=temp.next + prev=temp.next.next + temp.next=prev + self.head=temp + +list=circle() +n,k=map(int,input().split()) +for i in range(1,n+1): + list.insert(i) +print("Numbers standing in the josephus circle:") +list.print_list() +print("\n") +list.eliminate(n,k) +print("The last standing one is: ",end="") +list.print_list() \ No newline at end of file From bd650ba070df8ab1d341f5f54105415833220705 Mon Sep 17 00:00:00 2001 From: garvit088 Date: Mon, 17 Oct 2022 20:55:57 +0530 Subject: [PATCH 3/3] Delete josephus_circle.py --- algorithms/linkedlist/josephus_circle.py | 56 ------------------------ 1 file changed, 56 deletions(-) delete mode 100644 algorithms/linkedlist/josephus_circle.py diff --git a/algorithms/linkedlist/josephus_circle.py b/algorithms/linkedlist/josephus_circle.py deleted file mode 100644 index 0bf308b..0000000 --- a/algorithms/linkedlist/josephus_circle.py +++ /dev/null @@ -1,56 +0,0 @@ -''' -Josephus Circle: -Let there be N 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. -''' -class node: - def __init__(self,data): - self.data=data - self.next=None - -class circle: - def __init__(self): - self.head=None - - def insert(self,data): - new=node(data) - new.next=self.head - temp=self.head - if self.head==None: - new.next=new - self.head=new - else: - while temp.next!=self.head: - temp=temp.next - temp.next=new - - def print_list(self): - temp=self.head - while temp: - print(temp.data) - temp=temp.next - if temp==self.head: - break - - def eliminate(self,n,k): - ''' - This function eliminates the kth number - from the list - ''' - temp=self.head - for _ in range(n-1): - for _ in range(k): - temp=temp.next - prev=temp.next.next - temp.next=prev - self.head=temp - -list=circle() -n,k=map(int,input().split()) -for i in range(1,n+1): - list.insert(i) -print("Numbers standing in the josephus circle:") -list.print_list() -print("\n") -list.eliminate(n,k) -print("The last standing one is: ",end="") -list.print_list() \ No newline at end of file