Question

I need help writing a code in Java or C or python that follows the following...

I need help writing a code in Java or C or python that follows the following requirements.

1. It prompts the user to enter values of the four integers (a, b, c, and M in this order) and prints out the first 100 elements (?0, ?1, ?2, …, ?99) of the sequence produced by the linear congruential random number generator for these parameters. Use a for loop and the following update formula: x = (a * x + b) % M . Print the sequence elements in a table with 10 values per line.

2. The second part of this assignment is to implement Floyd's algorithm (described below) for finding the cycle length. First, you need to find a point on the cycle (Phase (i)). Then, you need to compute the cycle length (Phase (ii)). Output the cycle length.

We can determine the cycle length by using Floyd's algorithm for finding the cycle length or period. Floyd's algorithm consists of two phases: (i) find a point on the cycle, and (ii) compute the cycle length.  Phase (i). To find a point on the cycle, we use the following idea: run two versions of the generator concurrently, one iterating twice as fast as the other, until both versions have the same value. At some point, both of them are on the cycle and we have a race on the cycle, with the gap between the faster one and the slower one shrinking by one on each iteration. Eventually, the faster one catches up to the slower one, and the two generators are at the same point on the cycle. The following table contains an example of the process with a = 22, b = 1, c = 0, and M = 72. In this example, the cycle is 3-67-35-51-43-11-27- 19-59-3, and its length is 9. Somewhat coincidentally, this happens to be equal to the number of steps until the fast and slow generators first meet, but this will not be true in general. The fast generator (denoted by F) happens to be at 51 when the slow one (denoted by S) first hits the cycle at 3. The fast one is six steps behind the slow one, and catches up, one step at a time

Phase (ii). Once we know a value on the cycle, one more trip around the cycle (back to that same value) will give us the cycle length. Continuing the example above, we discovered from Phase I that 27 is in the cycle. After another 9 iterations we are back at 27, so we conclude the cycle length is 9 (as illustrated in the table below)

Sample run 1: Please enter values of a, b, c, and M in this order: 22 1 0 72

The first 100 elements of the sequence:

0 1 23 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27 19 59 3 67 35 51 43 11 27

Cycle length is 9

#######################################################################################

Sample run 2:

Please enter values of a, b, c, and M in this order:

123 456 789 1000000

The first 100 elements of the sequence:

789 97503 993325 179431 70469 668143 182045 391991 215349 488383 71565 802951 763429 902223 973885 788311 962709 413663 881005 364071 781189 86703 664925 786231 706869 945343 277645 150791 547749 373583 951165 993751 231829 515423 397485 891111 607109 674863 8605 58871 241589 715903 56525 953031 223269 462543 893245 869591 960149 98783 150765 544551 980229 568623 941085 753911 731509 976063 56205 913671 381989 985103 168125 679831 619669 219743 28845 548391 452549 663983 670365 455351 8629 61823 604685 376711 335909 317263 23805 928471 202389 894303 999725 966631 896069 216943 684445 187191 24949 69183 509965 726151 317029 995023 388285 759511 420309 698463 911405 103271

Cycle length is 50000

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

Answer:

Note: As per the specifications, the program can be done in either Java or C or Python. So, the program is given in Java.

Program code screen shot:

Add a comment
Know the answer?
Add Answer to:
I need help writing a code in Java or C or python that follows the following...
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
  • mips add matrix Can anyone help me with adding matrix in MIPS? This is the code...

    mips add matrix Can anyone help me with adding matrix in MIPS? This is the code given. We will be using it to run it in spim-cache Write an assembly code program to perform addition between two matrices A and B, and store the result into a matrix C, i.e, 1 2 3 4 51 [26 27 28 29 301 27 29 31 33 35 6 7 8 9 10 31 32 33 34 35 37 39 41 43 45...

  • Hi, I need help writing a code for this. The language is python 3, and we...

    Hi, I need help writing a code for this. The language is python 3, and we cannot use things like break, continue, exit(), lambda, map, filter, raise, try, except, and assert in our code. Thank you! Implement one of the sorting algorithms from class, either bubble, selection or quick sort with the following modification. Call the function "color_sort(the_list)" Each element is now a list (or tuple) with two elements. The first element is the number value that you need to...

  • The task involves writing a C++ program that determines the prime numbers between 1 and 100....

    The task involves writing a C++ program that determines the prime numbers between 1 and 100. The steps you should follow to identify the prime numbers are the following. 1. The number 1 is not a prime number, so it should be scratched. 2. Starting from the first prime number, which is 2, you scratch all the numbers that are the multiple of 2. You should not scratch out 2 itself. 3. The next number in the sequence after the...

  • C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with...

    C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Modify the following code in order for it to perform the following operations: *This code is...

    Modify the following code in order for it to perform the following operations: *This code is meant to simulate Floyd's cycle finding algorithm* A) Start with two pointers at the head of the list. We'll call the first one tortoise and the second one hare B) Advance hare by two nodes. If this is not possible because of a null pointer, we have found the end of the list, and therefore the list is acyclic C) Advance tortoise by one...

  • I need help writing this C code. Here is the description: Let's say we have a...

    I need help writing this C code. Here is the description: Let's say we have a program called smart typing which does two things: The first letter of a word is always input manually by a keystroke. When a sequence of n letters has been typed: c1c2...cn and the set of words in the dictionary that start with that sequence also have c1c2...cnc then the smart typing displays c automatically. we would like to know,  for each word in the dictionary,...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • I. Use the DNA sequence below, which encodes a prokaryotic gene to answer the following questions....

    I. Use the DNA sequence below, which encodes a prokaryotic gene to answer the following questions. 11 21 31 41 51 71 81 61 91 CGTAATTGAG GAGGTAGTTG ACGTATGAAT AGTTAACGTA CGGGGGGGAA 101 111 121 131 141 ACCCCCCCTT TTTTTTTTTC GAGCAATAAA AGGGTTACAG ATTGCATGCT a) Write down the corresponding sequences, find them in the sequence above and label them: -35 Consensus sequence: _(label as -35) -10 Consensus sequence (Pribnow box): _ __ (label as Pribnow) Shine-Dalgarno sequence in corresponding mRNA: __ (label as SD)...

  • Hallo! I have a problem in my code, I need help please in java code. My...

    Hallo! I have a problem in my code, I need help please in java code. My task is : Write a program that can read a text from a file and do frequency analysis on the letters that occur . To store information (frequency) you must use an array. Tip! The place where you collect the frequency of eg the letter A is the equivalent of the ASCII code of "A" ie 65. The array should be 127 elements. For...

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