How to determine whenether elements of an array are prime numbers

Discussion in 'MATLAB' started by Freek, Feb 22, 2010.

  1. Freek

    Freek Guest

    Hello,

    I am trying to code a program in Matlab that determines which elements of an array are prime numbers, of course without using the mathlab function 'isprime', because that would be too easy ;) The main problem I have is how exactly to read out the array, I'm just a bit stuck.

    I already programmed a program that determines wheneter an given integer is a prime number, but am having diffuculties to adapt the program so that it also accepts arrays (for instance input: [19, 56, 99], output should be like [1,0,0,1] ).

    Here's the program I already coded, which checks just 1 number to see if it's a prime number:

    function p = checkprime(n)
    p = 0;
    counter= 0;
    divisor = n;
    while divisor > 1,
    divisor = (divisor - 1);
    p = n / divisor;
    if p == floor(p);
    counter = counter + 1;
    end
    end
    if counter > 1
    disp ('0');
    else disp ('1');
    end

    As you can see, the program is pretty straight forward.
    It starts checking whenether the number is divisiable trough the entered value, minus 1. For example the number 6, it starts with 6/5 then 6/4 etc until 6/1.
    The answers to these solutions are then floored and compared to the unfloored answer. If it's the same, it adds +1 to the 'counter'. The counter should only have a max value of 1 if the number is a prime number (6/1=6, floored also 6) and if not, it's not a prime number...

    Now I only need to adapt my program in a way it also accepts arrays, like described above.

    Any help would be very much appriciated !

    Thanks in advance for the trouble to be taken !
     
    Freek, Feb 22, 2010
    #1
    1. Advertisements

  2. Freek

    Nathan Guest

    One way is to use a for loop outside of your while loop and index the
    array with the for loop:
    function [tfp] = checkprime(n)
    p = 0;
    divisor = n;
    tfp = zeros(size(n)); %assume not prime
    for ii = 1:length(n)
    counter= 0;
    while divisor(ii) > 1,
    divisor(ii) = (divisor(ii) - 1);
    p = n(ii) ./ divisor(ii);
    if p == floor(p);
    counter = counter + 1;
    end
    end
    if counter > 1
    tfp(ii) = 0;
    else tfp(ii) = 1;
    end
    end
    end
     
    Nathan, Feb 23, 2010
    #2
    1. Advertisements

  3. Freek

    Matt Fig Guest




    I didn't try out your code, but why just put a FOR loop in there around the engine?

    for ii = 1:length(n)
    divisor = n(ii);
    % The rest of your program.
    end
     
    Matt Fig, Feb 23, 2010
    #3
  4. Freek

    Freek Guest

    Thanks for your replies ! Really appriciate it !

    @Matt: I tried that, but I think because I used disp (‘0’) / disp (‘1’) to display if the given value is a prime number, that doesn’t work properly with arrays. Because it only works good for the first number of the array, but every next number displays a 0 as output…

    @Nathan: Thanks you very much ! Your program works like a charm ! Tough I adapted it a bit so that it displays the results on 1 line, instead of below each other.
    I'm now trying to optimize the program so that it works more efficient, because it takes very long to determine whenever large numbers are prime numbers or not. I already adapted the program in a way that if the given input value is an even number, it should display an 0 right away, since even numbers never are prime numbers, so that saves time, but only with even numbers. Now with odd numbers, I was thinking about making an restriction that the program should stop testing whenever if the input value is a prime number, after the counter hit a value of 2. Because if the counter has a value great than 2, you are a 100% sure it's not a prime number, since a prime number can only be divided by itself an trough one. And since my program starts counting at value-1, the counter should have a value of 1 if the value is a prime number.
    I tried the following, but it isn't working, the program keeps continuing testing the value even after the counter has hit a value equal to 2 or greater.

    function f = checkforprimeV2 (n)
    divider = n;

    for ii = 1:length(n)
    counter= 0;
    check = n(ii) / 2;
    check2 = floor(check);

    if check ~= check2

    while divider(ii) > 1 && counter <= 1 ,
    divider(ii) = (divider(ii) - 1);
    p = n(ii) / divider(ii);

    if p == floor(p)
    counter = counter + 1;
    end
    end

    else
    counter = 2;
    f(ii) = 0;
    end

    if counter > 1
    f(ii) = 0;

    else
    f(ii) = 1;
    end
    end
    end

    Any help would be appreciated :) !

    Thanks again !
     
    Freek, Feb 23, 2010
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.