Question

You will write a program using 2 threads to calculate the Fibonacci number. Each thread will...

You will write a program using 2 threads to calculate the Fibonacci number. Each thread will process one of the values used to calculate the result for the last iteration of the calculation. Each thread will use a recursive function to calculate its value and store its result in a global array. Each thread will return its results in a global array. The main program must wait for both threads to complete before exiting (join method must be used). The main program adds both values from the global array to calculate the value. In python.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We use the formulae of fibonacci that consists of no overlaps, ie

Fn = 4Fn-3 + Fn-6

This way we avoid computing useless values again and again. Comments are provided as required.

Code

#!/usr/bin/python3

import threading

class myThread (threading.Thread):
   def __init__(self, threadID, name, counter):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name
      self.counter = counter
   def run(self):
      print ("Starting " + self.name)
      threadLock.acquire()
      fibonacci(self.counter)
      threadLock.release()

def fibonacci(counter):
   if counter < 0:
       return 0
   elif fib[counter] == -1:
       fib[counter] = 4*fibonacci(counter - 3) + fibonacci(counter - 6)
   return fib[counter]

threadLock = threading.Lock()
threads = []

print("Enter n : ", end = "")
n = int(input())
fib = [-1 for x in range(max(7, n + 1))]
fib[0] = 0; fib[1] = 1; fib[2] = 1; fib[3] = 2; fib[4] = 3; fib[5] = 5; fib[6] = 8

# Create new threads
thread1 = myThread(1, "Thread-1", n - 1)
thread2 = myThread(2, "Thread-2", n - 2)

# # Start new Threads
thread1.start() ;thread2.start()

# # Add threads to thread list
threads.append(thread1) ;threads.append(thread2)

# Wait for all threads to complete
for t in threads:
   t.join()
ans = fib[n - 1] + fib[n - 2]

print("Fibonacci(", n, ") : ", ans)
print ("Exiting Main Thread")

Add a comment
Know the answer?
Add Answer to:
You will write a program using 2 threads to calculate the Fibonacci number. Each thread will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a c program to implement threads. Accept an array of 40 integers, write a thread...

    Write a c program to implement threads. Accept an array of 40 integers, write a thread method named sum of array elements. Divide the array into 4 equal elements and call threads to find the sum of each part. Store the sum in a variable called “arrays'. Print the sum in the main function. (10 points)

  • How to code a program in java language were 2 threads run Thread #1 - IntegerSorter...

    How to code a program in java language were 2 threads run Thread #1 - IntegerSorter class - sorts 100k random integers (values ranging from 0-100,000) Thread #2 - StringSorter class - sorts 30k element String array Driver.java, which includes “main” and invokes both threads

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • I want to write a C++ program which starts 2 threads. The first thread periodically generates...

    I want to write a C++ program which starts 2 threads. The first thread periodically generates a random number and adds it to the total. The second thread periodically generates a random number and subtracts it from the total. The main thread periodically monitors the value of the total. if the total exceeds the maximum value, the program terminates and alerts the user that the maximum has been exceeded. If the total undercuts the minimus value, the program terminates and...

  • Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formal...

    Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formally, it can be expressed as: fib0-0 fibl-1 fibn-fibn-1+fibn-2 Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in...

  • ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help...

    ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help of an integer array of size 100 (elements of this array will be digits of the Fibonacci number). When the function is called to find , it will calculate all Fibonacci numbers from to using the basic formula . To add two Fibonacci numbers, the function will add elements of two arrays corresponding to and and store their sums in the array corresponding to...

  • C program: 1: Write a program that uses 5 threads. Initialize a shared variable with a...

    C program: 1: Write a program that uses 5 threads. Initialize a shared variable with a value of 100. Each thread must add its Thread ID (tid) to the shared variable. Once a thread has done the addition, print the ID of the thread. [Note: thread does the printing] Output the value of the shared variable once all threads have finished incrementing it. [Note: main code does the printing] Provide a text file with your comments on the results of...

  • Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive...

    Using C programming please 5. Write a multithreaded program to do the following: Accepts two positive integers from the command line as the amounts to withdraw a. and deposit from an account. b. Creates two threads to perform withdraw and deposit operations repectively. Passes the amount to withdraw/deposit as the parameter to thread's runner function c. Waits for both threads to terminate. Since both threads need to update the account balance, it is necessary to protect sections. You may use...

  • Write a program that computes the Fibonacci number for some input integer. (See segments 7.37 –...

    Write a program that computes the Fibonacci number for some input integer. (See segments 7.37 – 7.41 in your book for some info on the Fibonacci sequence or you can look it up on the Internet). The sequence is defined as the first two elements are each 1, after that each element in the sequence is the sum of the previous two elements. The first few numbers of the sequence are 1,1,2,3,5,8,13,21,34,55,… Your assignment is to write a program with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT