Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can this be written in python please, the seconds photo is to show what the prof gave us.. Thanks! Question 1 (6 points): Purpose: To
Can this be written in python please, the seconds photo is to show what the prof gave us.. Thanks!
Question 1 (6 points): Purpose: To practice reading from a file and using a dictionary as a mapping Degree of Difficulty: Easy Caching is a very powerful technique in computer science. The technique simply involves recording the result of a complex calculation rather than re-computing that result every time it is needed. It may or may not surprise you to hear that there is no known formula for determinining whether or not a given number N is prime or not. More or less, the only way to know is to try dividing N by all integers between 1 and itself and seeing if any of them yield a remainder. For large N. this is a time-consuming computation, and therefore a good example of something that might be cached for some problems. For this question, your task is to write a program that takes a given set of numbers, determines whether each of them is prime, and caches the result using a dictionary Provided Files On the course website, you will find a starter file that already contains an implementation of a function called is_prime ). which accepts a positive integer and returns a Boolean value indicating whether or not that integer is prime. You can keep this provided function and use it in your program. You will also find a sample input file, numbers.txt. It contains some positive integers, one per line of the file. You can use this to test your program, but keep in mind that your program must work for ANY similarly- formatted input file. Write a Function Write a function that takes a file name as its only parameter. The function should open the given file, read the numbers in it, and call the provided is prime() function to determine whether each number is prime. The results of this process should be stored in a dictionary. The keys for this dictionary should be the numbers from the file, and the value for each key should be the Boolean result indicating whether the matching key is a prime number or not. The function should return this dictionary. Print the results Call the function you wrote, using "numbers.txt" as the argument. Print the returned dictionary to the console. Sample Run Your output might look something like this: {1: False, 2: True, 3: True, 4: False, 5: True, 6: False, 7: True, 8: false, 9: False 10: False, 11: True, 15: False, 99: False, 100001: False, 100019: True, 99999989: True, 99999991: False} Edef is_prime(N): Determines whether a given positive integer is prime or not input: N, any positive integer. returns: True if N is prime, False otherwise. # special cases: if N = 1: return false # the biggest divisor we're going to try divisor = N // 2 while divisor > 1: if N % divisor = 0: # we found a number that is a factor of N return false divisor = divisor - 1 return TrueStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started