0

I am able to extract list of prime numbers but when list has negative numbers, or 0 or 1 the condition doesn't work. Please help me fix the bug in this code. Detailed step by step explaination for putting the condition is encouraged . Thank you

clear 
clc
%Matlab program to give prime numbers in a given interval Ignoring 0, 1  and negative numbers if in the input range
start = input('Enter a starting number range:');
end_point = input('enter an ending number:');

list = start:end_point;  % Create a list having the range

prime = []; % initiate empty prime vector, prime numbers will be added to this list
for i = (1:length(list))
    fac = 0; % calculates the number of factors
    if list(i) <= 1 %Checks whether the number is less than or equal to 1
            fac = fac+1 % Assigns factor 1 and hence the algorithm ignores them from prime list 
            break
    for j = 2:list(i)/2
            if list(i) <= 1 %Checks whether the number is less than or equal to 1
                fac = fac+1 % Assigns factor 1 and hence the algorithm ignores them from prime list 
                break

%         if j == [] 
%             fac = fac + 1
%             break
        elseif mod(list(i),j) == 0 % checks whether a number is prime or not from the list
            fac = fac+1;
        else 
            fac = fac+0;
        end
    end
    if fac <1  % No factors between 2 and number\_checked/2 hence puts it in prime list 
        prime = [prime,list(i)];
    else 
        continue
    end

    end

  • 1
    I would enforce the condition when you construct `list`. If `start` is less than 2, set it to 2. (If `end_point` is less than 2, you can quit altogether.) – beaker Jan 11 '20 at 14:56

1 Answers1

3

Here is a revised code based on yours. You have some issues using break and logic errors in checking the primes.

I have commented my code, so you can read it for better understand. Hope it can help you!

clear 
clc
%Matlab program to give prime numbers in a given interval Ignoring 0, 1  and negative numbers if in the input range
start = input('Enter a starting number range:');
end_point = input('enter an ending number:');

list = start:end_point;  % Create a list having the range

prime = []; % initiate empty prime vector, prime numbers will be added to this list
for i = 1:length(list)
    if list(i) <= 1 %Checks whether the number is less than or equal to 1 
      break
    elseif ismember(list(i),[2,3]) % check if it is 2 or 3. If yes, then added to prime
      prime = [prime, list(i)];
    else
      for fac = 2:list(i)/2 % fac is possible from 2 to list(i)/2 
        if mod(list(i),fac) == 0 % if fac is a fact of list(i), then it is not a prime, thus break the loop
            break
        else % otherwise, check if the next integer would be the factor
            fac = fac +1;
        end
      end
      if fac > list(i)/2  % No factors between 2 and number\_checked/2 hence puts it in prime list 
        prime = [prime,list(i)];
      end
  end
end

Additional:

If you want to use build-in function of MATLAB for primes, isprime() might be the one you like. In this case, you code can be simplified as below

clear 
clc
%Matlab program to give prime numbers in a given interval Ignoring 0, 1  and negative numbers if in the input range
start = input('Enter a starting number range:');
end_point = input('enter an ending number:');

list = start:end_point;  % Create a list having the range
prime = list(isprime(list));
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • `prime = [prime, list(i)]` is the least efficient way to extend an array. `prime(end+1) = list(i)` is significantly more efficient. [See here for an explanation](https://stackoverflow.com/a/48353598/7328782). – Cris Luengo Jan 12 '20 at 15:22
  • 1
    @CrisLuengo Yes, I agree. It is not the efficient way, and actually many places can be largely improved based on OP's original code, not only on the tricks of append data, but also the algorithm of finding primes ....I just want to keep the OP's code style and focus on the logic to show how to achieve the desired output – ThomasIsCoding Jan 12 '20 at 15:54
  • Thank you @ThomasIsCoding . I found my logical error. Your code was helpful. Thank you for the short code . – Darshit Thakar Jan 14 '20 at 11:27
  • Thank you @CrisLuengo for suggesting an efficient code. – Darshit Thakar Jan 14 '20 at 11:27
  • @DarshitThakar you are welcome! If you are happy with my answer or think it is helpful, please feel free to upvote/accept, thanks :) – ThomasIsCoding Jan 14 '20 at 11:50