Darshit Thakar
Darshit Thakar

Reputation: 45

Generate a list of prime numbers from the input range by user in Matlab. Condition is ignore negative numbers, 0 and 1 if in list

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

Upvotes: 0

Views: 278

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102251

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));

Upvotes: 3

Related Questions