Question

Implement the following Python functions. These functions take advantage of the generalized Euclid's lemma to make...

Implement the following Python functions. These functions take advantage of the generalized Euclid's lemma to make it possible to generate a random number within a specified range. Your implementations must be extremely efficient, and must handle very large inputs, as shown in the examples below. Implementations that perform exhaustive, exponential-time searches will receive no credit.

a. Implement a function closest(t, ks) that takes two arguments: a target integer t and a list of integers ks. The function should return the integer k in ksthat is closest to t (i.e., the integer k in ksthat minimizes the absolute value of the difference |t − k| between the two numbers). This will serve as a helper function for subsequent problems in this assignment.

>>> closest(5, [1,3,4,9,10])

4

>>> closest(8, [1,3,4,9,10])

9

b. Implement a function findCoprime(m) that takes a single positive integer argument m and returns an integer b where b > 1 and b is coprime with m. Your implementation does not need to return exactly the same answers as you see in the example outputs. However, the output generated by your implementation must be coprime with the input. You need to use facts about coprime numbers.

>>> findCoprime(10)

7

>>> findCoprime(100)

63

>>> findCoprime(872637825353262)

545398640845789

>>> findCoprime(2**200)

1004336277661868922213726307713226626576376871114245522063361

>>> gcd(findCoprime(2**100000), 2**100000)

1

c. Implement a function randByIndex(m, i) that takes two positive integer arguments: m represents the upper bound of random numbers to be generated, and i represents an index specifying which random number in the sequence should be generated. You may assume m ≥ 4 and that 1 ≤ i ≤ m − 1. The function must return the i th "random" number in a permutation of the numbers {0, ..., m − 1} by implementing the simplified linear congruential generator with a well-chosen coprime.

>>> [randByIndex(10, i) for i in {0,1,2,3,4,5,6,7,8,9}]

[0, 7, 4, 1, 8, 5, 2, 9, 6, 3]

>>> [randByIndex(77, i) for i in range(0,76)]

[ 0, 48, 19, 67, 38, 9, 57, 28, 76, 47, 18, 66, 37, 8, 56, 27, 75, 46, 17, 65, 36, 7, 55, 26, 74, 45, 16, 64, 35, 6, 54, 25, 73, 44, 15, 63, 34, 5, 53, 24, 72, 43, 14, 62, 33, 4, 52, 23, 71, 42, 13, 61, 32, 3, 51, 22, 70, 41, 12, 60, 31, 2, 50, 21, 69, 40, 11, 59, 30, 1, 49, 20, 68, 39, 10, 58]

>>> randByIndex(2**200, 2**99+1)

1004336277661868922213726307713860451876490985814993873666049

Your implementation does not need to return exactly the same answers as you see in the example outputs. However, the output generated by your implementation must produce a permutation when used in a comprehension, as in the examples, and must work on very large upper bounds and indices.

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

def closest(t, ks):
    return min(ks, key=lambda p: abs(p-t))

print(closest(5, [1,3,4,9,10]))
print(closest(8, [1,3,4,9,10]))

def findCoprime(x):
    for i in range(2,x):
        if math.gcd(i, x) == 1:
            return i

print(findCoprime(100))
print(findCoprime(872637825353262))
print(2**200)
print(findCoprime(2**200))



def randByIndex(n,i) :
    return (i*findCoprime(n))%n


print([randByIndex(10, i) for i in range(0,10)])

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Implement the following Python functions. These functions take advantage of the generalized Euclid's lemma to make...
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
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