We have to write a function called next_prime that takes a scalar positive integer input n. Use a while-loop to find and return k, the smallest prime number that is greater than n. we may use isprime function. plz help..
MATLAB: Next prime number using While loops
eratosthenesprime numbersoft-lockwhile loop
Related Solutions
Method 1 This functionality does not run in MATLAB and can be used only via MuPAD Notebook Interface.
- To create an MuPAD interface use the following code
mupadnb = allMuPADNotebooks
Then a interface screen shall pop up where you can proceed by using the nextprime(num) function.
Method 2
- You could also create a user defined function to compute the next prime number. This function takes only a non-negative integers as an argument
function p = nextprime(n)if (isprime(n)) p=n;else while(~isprime(n)) n=n+1; end p=n;endend
Ok, it looks like you are still interested in this, so here is an approach targetted more at what I hear from your last comment.
Simple would be, if you just want to find all twin primes less than some value, just use primes to get all primes, then find all primes that are exactly 2 larger than the one before.
N = 1e7;P = primes(N);ind = find(diff(P) == 2);TP = [P(ind)',P(ind+1)'];size(TP)ans = 58980 2TP(1:10,:)ans = 3 5 5 7 11 13 17 19 29 31 41 43 59 61 71 73 101 103 107 109
But there is no formula to find all such twin primes, nor to predict when a twin prime pair will occur, except that it must be a pair of the form 6*m+[-1 1].
Yes, you could use isprime to test each pair of numbers of that form, but that would be extremely inefficient, far less good than just calling primes directly.
So, is there something slightly better? For example, could we write a variation of a sieve, targetted specifically at twin primes? For example, suppose I wanted to find all twin primes less than 1e9? primes(1e9) is probably too big for me to do on my computer. But I know that twin primes must be of the form 6*m +/-1.
tic% find all twin prime pairs where both are less than N
N = 1e9;Plist = primes(sqrt(N));% Keep the small twin primes from Plist
twin_primes = Plist(diff(Plist) == 2);twin_primes = [twin_primes;twin_primes+2]';% We don't need to check 6*m-1 for divisibility by 2 or 3.
Plist(1:2) = []; % m will correspond to the index of a potential twin prime, 6*m-1.
% At the end of this comutation, if m(k) is true, then
% 6*m(k) +/- 1 will be a twin prime. Note that all of this will miss the
% very first twin prime pair, [3 5]. But all other twin prime pairs are of
% the form 6*m +/- 1.
m = true(1,floor((N-2)/6)); for p_i = Plist % which elements of m correspond to 6*m-1 that are divisible by p_i?
% Find them using the modular inverse of 6, modulo p_i.
p_i_inv = minv(6,p_i); m1 = p_i_inv; if p_i == (6*m1 - 1) m1 = m1 + p_i; end m(m1:p_i:end) = false; m2 = mod((p_i - 1)*p_i_inv,p_i); if p_i == (6*m2 - 1) m2 = m2 + p_i; end m(m2:p_i:end) = false; endm = find(m); twin_primes =[twin_primes;[6*m-1;6*m+1]'];toc
So reasonably fast.
Elapsed time is 9.773307 seconds.
This ran pretty quickly on my computer, since the only RAM it required was a vector of size N/6 bytes. So for N=1e9, m only required 0.17 gigabytes. And the loop was also pretty efficient.
Note that the above code uses my minv tool, which is found in my vpi toolbox, although when used on doubles, it returns double precision output.
whos m twin_primes Name Size Bytes Class Attributes m 1x3424019 27392152 double twin_primes 3424506x2 54792096 double
So it found every one of the 3.4 million twin prime pairs for which neither of them exceed 1e9. But since it did not need to explicitly test any of them explicitly for primality, nor did it need to generate the entire list of primes below 1e9, the computation was actually pretty fast. While my computer is moderately old, I'd guess I could easily push N out further out than 1e10. (I just tried it for N=1e10, and it took 110 seconds to find all 27412679 twin prime pairs less than 1e10. Not bad for a 10 year old computer.)
I'm not sure if this will be of any real value to you, but it will indeed generate all such twin primes less than N.
Best Answer