MATLAB coding. I really appreciate if any body can help me with these problems??

2 views (last 30 days)
I've just ignored this 3 problems (out of 9) in my final today and I wonder someone could help me to solve these problems so I can understand how to do it.
1/
Suppose we have a job that will take 20 hours to complete, and requires the use of a special (and expensive!) type of battery. It is known from exhaustive testing that the probability one of these batteries will wear out in (exactly) one hour is 0 .1, in two hours is 0.4, in three hours is 0.3 and in four hours is 0.2. For each 5 =< n =< 20 , let Pn denote the probability that exactly n batteries will be required to finish the job. Write a program that uses simulation to approximate the probabilities Pn . Perform a large number of simulated jobs, each time counting how many batteries were needed to complete the necessary 20 hours of work. Whenever a new battery is required, use a random number to determine how long it will last. Assume that as soon as a battery wears out, it is immediately replaced with a new one. Approximate each Pn as the fraction of trials when exactly n batteries were required to finish the job.
2/
The goal is to write a program that will determine whether or not a given number is prime.
Let N and D be positive integers. D is called a divisor of N if there is a positive integer Q such that N = QD. An integer N >= 2 is called a prime number (or simply prime) if it has no divisors other than 1 and N itself. Equivalently, N is prime if it is not the product of two smaller positive integers. (For example, N = 10 is not prime, since10 = 2*5 , but N = 13 is prime.)
To determine whether or not an integer N is prime, we could successively test each of the
numbers P = 2,3, ... ,N - 1, stopping if we encounter a divisor. But note that if an even number is a divisor of N, then so is P = 2 . Thus, we can restrict our testing to the number 2, and to the odd integers that are less than N . Moreover, we can stop at sqrt(N) , since if there is a divisor that is greater than sqrt(N), then there must also be one that is less.
Write a program that will take an integer N as input, and determine whether or not it is prime.
Your program should use a while loop to search for a nontrivial divisor of N, successively testing the numbers P = 2,3,5,7, , up to the largest odd integer less than or equal to N , if necessary. (The MATLAB function rem will be useful here.) If a divisor is found, the loop should immediately terminate. In this case the program should report that N is not prime, and print this divisor. If no divisor is found, the program should report that N is prime.
Test your program on the integers: N = 3,358,247, N = 193,707,721, N = 2,147,483,647, and N = 3,524,084,471.
3/ In this problem, we will use a method called the Sieve of Eratosthenes (276-196 BC) to produce a list of all the prime numbers less than or equal to a given integer N. This approach begins with a list of the numbers 2 through N, and then proceeds to systematically delete all the non-prime numbers from the list.
To see how it works, consider the case N = 30 as an example. Start with the list
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
First delete from the list all numbers that are greater than 2 and divisible by 2, getting
2 3 5 7 9 11 13 15 17 19 21 23 25 27 29
Now delete all numbers that are greater than 3 (which is the number in the new list that immediately follows 2) and divisible by 3, getting
2 3 5 7 11 13 17 19 23 25 29
Then delete all numbers that are greater than 5 and divisible by 5, getting
2 3 5 7 11 13 17 19 23 29
Note that, at this point, all the numbers that have survived must be prime. (This is because the number following 5 in the list is 7, and since 7 > sqrt(30) , it cannot divide any of the remaining numbers.)
Write a program that implements the Sieve of Eratosthenes to find all the prime numbers that are less than or equal to a (user-supplied) integer N. You should be able to carry this out in just a few lines of code, without the need for conditional statements. (The MATLAB function rem will be useful here.) Be sure to stop as soon as the relevant integer is greater than sqrt(N) (see example above). Test your program when N=30, print out your results, and compare them with the example given above. Then find (but don’t display!) all the prime numbers that are less than or equal to N=1,000,000. How many are there? How many are less than 500,000? What is the 10,000th prime? What are the last 6 primes that are less than or equal to 1,000,000?
(In 1914, D.N. Lehmer was able to use these techniques (together with a great deal of effort by himself and many others) to find all the prime numbers up to 10,000,000. Running your program, MATLAB will be able to do all this in a couple of minutes!)
I really appreciate your help

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!