•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Status Updates

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Algorithm in pascal to find out if a number is prime or not finds all odd numbers prime

prime

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

### #1 AsdasDsabas

AsdasDsabas

CC Lurker

• New Member
• 4 posts

Posted 24 March 2014 - 10:08 AM

I created an algorithm to find out if a number is prime or not and then transfer the prime numbers to a different array. However, it finds all odd numbers as prime. What is wrong? How do I fix it?
Here's the code: http://freetexthost.com/mzfydpjt6z

Edited by Roger, 24 March 2014 - 10:18 AM.
title

### #2 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts

Posted 24 March 2014 - 10:46 AM

Okay, let's look at your code here (properly indented):

```program pirminiai;
var
M:array[0..3] of word;
T:array[0..10] of word;
i,u,x,e:integer;

begin
e:=(-1);
for i:= Low(M) to High(M) do
begin
M[i]:=x;
end;

for i:= Low(M) to High(M) do
if M[i]=2 then
begin
e:=e+1;
T[e]:=M[i];
end
else
begin
for u:=2 to (M[i]-1) do
begin
if (M[i] mod u)=0 then
break
else
e:=e+1;
T[e]:=M[i];
break;
end;
end;
for i:=Low(T) to High(T) do
write(T[i],' ');
end.

```

First question: have you traced through your code? Consider what happens if you enter 9. Your loop of u:=2 to (M[i]-1) does the following:

It check whether 9 is divisible by 2 (it's not). It then increments e and adds 9 to array T and breaks. So the result is that that all you checked was divisibility by 2.

What you should probably do is use a boolean isPrime that is set to true before the u loop, and is set to false if divisibility happens. Then after the loop, you can check isPrime to determine if it needs to be added to array T.

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

### #3 AsdasDsabas

AsdasDsabas

CC Lurker

• New Member
• 4 posts

Posted 24 March 2014 - 11:04 AM

I'm sorry for a stupid question but I'm new to pascal. Why does it only check divisibility by 2? I wanted it to check divisibility by all the numbers between 2 and my value minus one. How do I do that?

### #4 WingedPanther73

WingedPanther73

A spammer's worst nightmare

• Moderator
• 17757 posts

Posted 25 March 2014 - 05:52 AM

Let's do a manual trace of your code. We'll assume M[0] is 9, and e=-1.

```  for i:= Low(M) to High(M) do
if M[i]=2 then
begin
e:=e+1;
T[e]:=M[i];
end
else
begin
for u:=2 to (M[i]-1) do
begin
if (M[i] mod u)=0 then
break
else
e:=e+1;
T[e]:=M[i];
break;
end;
end;

```

Line 1 sets i=0;

Line 2 evaluates to false, and we jump to Line 8

Line 9 sets u to 2

Line 11 evaluates to false (9 mod 2 = 1) and we jump to Line 13

Line 14 increments e to 0

Line 15 stores 9 in T[0].

Line 16 calls break, and we return to line 1 and increment i.

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download