Created
November 21, 2021 16:52
-
-
Save n0toose/cb6bc25d84a89120314656e35be629b7 to your computer and use it in GitHub Desktop.
Find n-th pair of prime numbers with a difference of two in MATLAB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 90; | |
disp(primePair(n)) | |
function p = primePair(n) | |
% Incredibly inefficient, use the primes vector instead. | |
% I just came up with this during an exam and didn't want | |
% to think about going over the vector correctly too much. | |
a = [ -2 0 ]; | |
counter = 0; | |
while counter ~= n | |
a(1) = a(1) + 1; | |
a(2) = a(2) + 1; | |
% isprime(a(2)) gets checked first. If it fails, | |
% it won't proceed to run isprime(a(1)), which will | |
% fail initially, as a(1) is a negative number. | |
% | |
% This could be worked around with the abs() function | |
% but I really gotta hand in this test and only have 54 minutes. | |
if isprime(a(2)) && isprime(a(1)) | |
counter = counter + 1; | |
end | |
end | |
p = a; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment