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
  • You may import the following library functions in your module: from fractions import gcd from math...

    You may import the following library functions in your module: from fractions import gcd from math import log from math import floor You may also use: • the .bit_length() method to efficiently obtain the bit length of an integer, • the abs() function for computing the absolute value of an integer, • and the // operator for integer division (you should avoid using / because it does not work for very large integers). Implement the following Python functions. These functions...

  • You may import the following library functions in your module: from fractions import gcd from math...

    You may import the following library functions in your module: from fractions import gcd from math import log from math import floor 'You may also use: • the built-in pow() function to compute modular exponents efficiently (i.e., ak mod n can be written in Python as pow(a,k,n)), You may also need to import your hw3 (will be attached below) 5. Implement a function makePrime(d) that takes a single integer argument d where d >= 1 and returns a probably prime...

  • In this problem you will implement an algorithm for computing all the square roots of a congruenc...

    In this problem you will implement an algorithm for computing all the square roots of a congruence class in ℤ/nℤ, given a complete factorization of n into its distinct prime factor powers (assuming all the prime factors are in 3 + 4ℤ). a) Implement a Python function sqrtsPrime(a, p) that takes two arguments: an integer a and a prime number p. You may assume that a and p are coprime. If p is not in 3 + 4ℤ or a...

  • You may import the following library functions in your module: from fractions import gcd from math...

    You may import the following library functions in your module: from fractions import gcd from math import floor from random import randint You may also use: • the built-in pow() function to compute modular exponents efficiently (i.e., ak mod n can be written in Python as pow(a,k,n)), • the sum() function returns the sum of a list of integers (sum(1,2,3,4) returns 10). problem 1 a. Implement a function invPrime(a, p) that takes two integers a and p > 1 where...

  • Python Lesson Assignment Implement the following 4 functions: fancy_min(a, b) Input: two formal parameters: a and...

    Python Lesson Assignment Implement the following 4 functions: fancy_min(a, b) Input: two formal parameters: a and b Output: return the minimum of a and b Notes: a and b can either be a number or None if a is None, return b if b is None, return a if both are None, return None otherwise return the minimum of a and b do not use the built in function min() fancy_max(a, b) Input: two formal parameters: a and b Output:...

  • Python Programing : Convert to binary - functions Write a program that takes in a positive...

    Python Programing : Convert to binary - functions Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a...

  • Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...

    Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions) Functions you need to implement: Define function initialize() that takes array lotterNumbers[] as a parameter and assigns...

  • Note: None of these functions should use cout. It is OK to use cout while debugging...

    Note: None of these functions should use cout. It is OK to use cout while debugging to check how your function is working, but your final submission should not contain cout in any function but main. Head ==== Name the source file for this section head.cpp. Write a function named "head" which takes an array of integers as an argument, and returns the first element in the array. Write a function named main which outputs the result of testing your...

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • Write a Python file containing the following functions. Also turn in the output from testing the...

    Write a Python file containing the following functions. Also turn in the output from testing the functions. All arguments to the functions may be hard-coded. Function 1 takes a list of strings as a parameter and returns a list of strings consisting of all the strings in the original list that have identical consecutive letters. For example: fun1 ([llama, good, cat, abba, abab, 112, dog]) returns [llama, good, abba, 112] Function 2 takes an integer (n), representing the number of...

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