Question

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

  • 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...

  • 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...

  • Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a...

    Problem 2 (USE C++) Assume you are given two functions: a function that returns –in a variable passed by reference- the GCD of all integer numbers in an array void GCD(int A[], int size, int &g) and another function that allows you to divide all numbers of an array by a given integer void Div(int A[], int size, int gcd) (assume the functions are in a library, therefore you don’t need to implement them, but just use them). 1. Write...

  • Exercise 2: Import a Python Math Module You can find a list of all of the...

    Exercise 2: Import a Python Math Module You can find a list of all of the math functions available in the Python.org Math Functions page found in the Resources. Select any math function(s) that you would like to use in a program. Do the following: Write a program that imports the math module and uses the function that you selected. (Hint: You will need to determine the arguments that must be passed to the function, return the result, and display...

  • 1.  When using a function in the Python standard library, do you need to import the library...

    1.  When using a function in the Python standard library, do you need to import the library into your program before using the function? 2.  How does the walk of a pseudorandom sequence differ from the walk of a truly random sequence? 3.  Is the computer capable of producing a truly random walk in a reasonable amount of time? 4.  Based on your research in the standard library documentation, what is the name of the function that can be used to generate random integer...

  • YOU MUST NOT MAKE ANY CHANGES TO THAT CODE package edu.buffalo.cse116; /** * Class that can...

    YOU MUST NOT MAKE ANY CHANGES TO THAT CODE package edu.buffalo.cse116; /** * Class that can be used to test if two integers are coprime. Numbers are considered coprime iff the largest divisor * shared by both numbers is 1. Identifying coprime (also called relatively prime) numbers is valuable for RSA public * key cryptography (and possibly other things, but the author has never seen any other uses). */ public class CoprimeNumbers { /** * Given two integers, this returns...

  • Create a Python script file called hw.py. Then import the module random: from random import *...

    Create a Python script file called hw.py. Then import the module random: from random import * Thanks in advance! Ex. 1. Write a function called cleanLowerWord that receives a string as a paramcter and retums a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this,...

  • C++ programming 6-1: User Defined Functions I the C++ cmath library contains several math-related functions. for...

    C++ programming 6-1: User Defined Functions I the C++ cmath library contains several math-related functions. for a general listing of functions contained within this library, review your text or visit Cplusplus.com (http://www.cplusplus.com/reference/cilbrary/cmath/) if you had the option to redesign the math library, what other functions would you add to it? for example, do you think a max function or average function might be useful? give a list of at least three functions you think are worth adding to the math...

  • Please answer the following question. Thank you! For this assignment you must write the following functions...

    Please answer the following question. Thank you! For this assignment you must write the following functions in Scheme: 1.2 Write a function called countdown that takes a positive integer and uses a do expression to display a sequence of numbers counting down to 1, each on its own line, then displaying the string "Blastoff!". > (countdown 5) 5 4 3 2 1 Blastoff!

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