can anyone tell me how to write this in LC3
thanks
for (i=0, i < 3001, i++)
{
if (i == 0)
A[i] = 0
A[i+1] = 0
else
A[i] = i
}
for (i = 1, i < 30,i++)
{
for (i+i; j < 3001)
{
if (A[i] != 0)
{
A[j] = 0
j= j+i
}
}
}
can anyone tell me how to write this in LC3
thanks
for (i=0, i < 3001, i++)
{
if (i == 0)
A[i] = 0
A[i+1] = 0
else
A[i] = i
}
for (i = 1, i < 30,i++)
{
for (i+i; j < 3001)
{
if (A[i] != 0)
{
A[j] = 0
j= j+i
}
}
}
actually that isn't even compileable C function. It needs braces around lines 3-6. And the loop starting at line 10 has several problems -- one of them is probably infinite loop at line 12 because the value of j may never get incremented.
It is also a horribly written function. Do you really want to check the same value of A about 3000 times? Since the value of i never changes during that loop it will check it AT LEAST 3,000 times, and probably a lot more times than that if the value of j never reaches 3001.
Actually I am new to C too...sorry. THE PROGRAM SHOULD GIVE ME PRIME NUMBERS BETWEEN 0 AND 3000 'Sieve of Eratosthenes'
THE FOLLOWING EXPLAINING THE 'Sieve of Eratosthenes' ALGORITHM
This is more detailed.
Start with a list of numbers, incorrectly assuming at first that
every number is prime (except for 0 and 1 of course, since those are
not prime, nor will they play a part in our algorithm). How do we go
about doing this? The method you will be using is to initialize an
array of integers to non-zero values. Although the typical sieve uses
a boolean 0 or 1 to represent if a number is primed, we will use 0 to
mean not prime and the number itself to mean prime. For instance:
PRIMES + 3 = 3
PRIMES + 4 = 4
.
.
.
PRIMES + 10 = 10
.
.
.
PRIMES + 3000 = 3000
So, to begin with we will have an array that looks like the
following:
0 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ......
Why are both of the first two numbers 0? Well, 0 and 1 are known to
not be included in the set of prime numbers. The way we will be
marking a number as 'not prime' is by zero'ing it out in our array.
So, all I have done is gone ahead and marked both '0' and '1' as not
prime by making their values 0.
The next step of the algorithm is just a huge loop. The idea is to
start at the first non-zero value and mark out all multiples of that
number in your array. Why? Anything that is divisible by a number
other than 1 or itself is *NOT* prime, so all multiples of a number
determined to be prime cannot be prime! To illustrate, we would start
at the number 2. Since 2 is not zero'd out, we assume it is prime and
mark out all multiples of 2.
0 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ......
X X X X X X X X X X
Which brings our array to look like the following:
0 0 2 3 0 5 0 7 0 9 0 11 0 13 0 15 0 17 0 19 0 21 0 23 ......
After this process is completed for the entire array, we simply go
back to our starting value of 2 and find the next value which is not
0. In our case this happens to be 3, so we then start this process
all over again.
0 0 2 3 0 5 0 7 0 9 0 11 0 13 15 0 0 17 0 19 0 21 0 23 ......
X X X
Which brings our array to look like this:
0 0 2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0 0 0 23 ......
If you take a quick look, you will notice that we have already
successfully found all the prime numbers between 2-23 inclusive.
I NEED TO IMPLEMENT THIS FROM 0 TO 3000.sO THE PROGRAM SHOULD GIVE THE PRIME NUMBERS BETWEEN 0 -3000
[/I]
actually that isn't even compileable C function. It needs braces around lines 3-6. And the loop starting at line 10 has several problems -- one of them is probably infinite loop at line 12 because the value of j may never get incremented.
It is also a horribly written function. Do you really want to check the same value of A about 3000 times? Since the value of i never changes during that loop it will check it AT LEAST 3,000 times, and probably a lot more times than that if the value of j never reaches 3001.
I would suggest you forget that C code you posted and start fresh with assembly code. Sorry, but I can't help you with LC3.
OK THANK YOU.I WROTE THE C CODE TO GIVE SOME KIND OF IDEA..SO I CAN FOLLOW THE STEPS..IF POSSIBLE CAN YOU HELP ME WITH THE C CODE...SO I CAN TRY TO WRITE IN LC3
THANKS
I would suggest you forget that C code you posted and start fresh with assembly code. Sorry, but I can't help you with LC3.
for (i=0, i < 3001, i++)
{
A[i] = i
}
for (i = 1, i < 30,i++)
{
int j;
for (j = i+i; j < 3001; j++)
{
if (A[j] != 0)
{
A[j] = 0
}
}
}
Ok here's the c code -- but still confusing so don't blaim me if its wrong :).