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 number that has exactly d digits. Your implementation should be sufficiently efficient to produce an output for d = 100 in a reasonably short amount of time. Implementations that perform an exponentially large exhaustive search, even if the algorithm is mathematically correct, will not earn full credit.
>>> makePrime(2)
47
>>> makePrime(100) 3908330587430939367983163094172482420761782436265274101479718696329311615357177668931627057438461519
hw3 functions
def closest(t, ks):
"""takes 2 arguments a target t and list of ints ks and returns in
k in
ks that is closes to t"""
return min(ks, key=lambda p: abs(p-t))
def findCoprime(m):
"""takes a single positive int m and returns an int b where b >
1 and b is
coprime with m""""
for i in range(2,x):
if math.gcd(i, x) == 1:
return i
def randByIndex(m, i):
"""takes 2 positive integer arguments m represents the upper bound
of random numbers
generated and i represents a index specifying which random number
in the sqeuence
should be generated"""
return (i * findCoprime(n)) % n
from fractions import gcd
from math import log
'''
Z = all integers
Problem 1(a)
4 * x + 1 = 9 (mod 17)
4 * x = 8 (mod 17)
4 * x = 4 * 2 (mod 17)
x = 2 (mod 17)
Solution: x = 2 + 17Z
Problem 1(b)
x = -6 (mod 13)
x = 7 (mod 13)
Solution: x = 7 + 13Z
Problem 1(c)
40 * x = 5 (mod 8)
0 * x = 5 (mod 8) # x * 0 must equal
0
Solution: NONE
Problem 1(d)
3 * x + 1 = 1 (mod 3)
3 * x = 0 (mod 3)
0 * x = 0 (mod 3)
Solutions: x in {0 + 3Z, 1 + 3Z, 2 + 3Z}
Problem 1(e)
5 * x + 7 = 13 (mod 29)
5 * x = 6 (mod 29)
5 * x = 35 (mod 29)
5 * x = 5 * 7 (mod 29)
x = 7 (mod
29)
#can cancel because 29 is prime and 5 = 0 (mod 29)
Solution: x = 7 + 29Z
Problem 1(f)
1 + 2 * x = 2 (mod 10)
2 * x = 1 (mod 10)
Solution: NONE
Problem 1(g)
17 * x + 11 = 300 (mod 389)
17 * x = 289 (mod 389)
17 * x = 17 * 17 (mod 389)
x = 17 (mod
389)
#can cancel 17 on both sides because 389 is prime)
Solution: x = 17 + 389Z
Problem 1(h)
650472472230302 * x = 1 (mod 8910581811374)
0 * x = 1 (mod 8910581811374)
Solution: NONE
Problem 1(i)
48822616 * x = 14566081015752 (mod 3333333333333333333333333)
48822616 * x = 48822616 * 298347 (mod
3333333333333333333333333) #48822616 and
3333333333333333333333333
x = 298347 (mod
3333333333333333333333333)
#are coprime, cancel 48822616 on both sides
Solution: x = 298347 + 3333333333333333333333333Z
'''
#--------------------------------------------------------------------------
# Problem 2
def closest(t, ks):
k = ks[0]
diff = abs(t-ks[0])
for y in ks:
if abs(t-y) < diff:
diff =
abs(t-y)
k = y
return k
#--------------------------------------------------------------------------
# Problem 3a
# Gather coprimes into a list, then return closest to (4/7)*m
def findCoprime(m):
coprimes = list()
# Method for odd numbers
if m % 2 == 1:
k = int(log((4/7)*m, 2))
coprimes.extend([pow(2,k),pow(2,k+1),pow(2,k-1)])
# Method for all numbers
# Find coprime close to (1/2)m, then stop iteration
for b in range( (m//2)+1, m ):
if gcd(m, (b // gcd(m, b))) == 1:
coprimes.extend([(b//gcd(m,b))])
break
# Method for all numbers
p = 4*m//7
#if(p < 3): p =
3
# p must be in {3,...,m-1}
while( gcd(p-1,m) != 1 ): # not coprime
p = (p * gcd(p-1,m))
coprimes.append((p-1) % m)
return closest(((m*4)//7)+1,coprimes)
#--------------------------------------------------------------------------
# Problem 3b
# Do not need to find closest coprime to (4/7)*m
# ...because findCoprime(m) already does
def randByIndex(m,i):
b = findCoprime(m)
return( (b*i) % m )
#--------------------------------------------------------------------------
#Probelm 4
def probablePrime(m):
for k in set(range(1,m.bit_length()+1)):
if randByIndex(m,k)>1: a =
randByIndex(m,k)
if (m%a)==0: return False
if gcd(a,m) != 1: return False
if pow(a,m-1,m) != 1: return False
return True
#--------------------------------------------------------------------------
#Problem 5
def makePrime(d):
#number of primes with d-digits
numPrimes = ((pow(10,d))//(log(pow(10,d)))) -
((pow(10,d-1)+1)//(log(pow(10,d-1)+1)))
#chance of being prime
chancePrime = numPrimes/((pow(10,d)+1) - pow(10,d-1))
#number of random numbers we need to check
kk = int(1/chancePrime) + 1
i = 1 #counter to iterate through random index list
# multiply kk by 10 to increase chances of randomly finding prime
number
for k in set(range(0,kk*10)):
n = 0
while(n == 0):
if( randByIndex(pow(10,d),i) >=
pow(10,d-1) ):
n =
randByIndex(pow(10,d),i)
i = i + 1
break
i = i + 1
if probablePrime(n): return n
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 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 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...
Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...
C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...
Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them. Map The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in-order in a new vector. It then returns a pointer to a new vector. You should pass your square function...
Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:AdditionSubtractionMultiplicationDivisionModuloExponentThe following source files are already complete and CANNOT be changed:MathQuiz.java (contains the main() method)OperationType.java (defines an enumeration for valid math operations and related functionality)The following source files are incomplete and need to be coded to meet the indicated requirements:MathQuestionable.javaA Random object called RANDOM has already been declared and assigned, and you must not change this assignment in any way.Declare and assign an interface integer called MAX_SMALL and assign it the...
Create a basic math quiz program that creates a set of 10 randomly generated math questions for the following operations:AdditionSubtractionMultiplicationDivisionModuloExponentThe following source files are already complete and CANNOT be changed:MathQuiz.java (contains the main() method)OperationType.java (defines an enumeration for valid math operations and related functionality)The following source files are incomplete and need to be coded to meet the indicated requirements:MathQuestionable.javaA Random object called RANDOM has already been declared and assigned, and you must not change this assignment in any way.Declare and assign an interface integer called MAX_SMALL and assign it 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...
Please place all of the following functions (defun …) into a SINGLE .lsp file As such, be sure to use the EXACT SAME function names and parameter numbers and orders I provide Write a function ONEFIB that takes a single parameter n and returns the nth Fibonacci number. Write a function ALLFIB that takes a single parameter n and returns a list of the first n Fibonacci numbers. Do not worry about efficiency here. HINT: You can use ONEFIB...