answersLogoWhite

0

Maybe not the most elegant way but it works.

public class Main {

public static void main(String[] args) {

int count = 0;

int number = 2;

while(count < 10) { int c=0;

for(int i = 2; i < number ; i++)

{

if (number%i == 0)

{

c=1;

break;

}

}

if(c==0)

{

System.out.print(number + " ");

count++;

}

number++;

}//end of while

} //end of main

} //end of class

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
ReneRene
Change my mind. I dare you.
Chat with Rene
More answers

bool is_prime (unsigned num) {

if (num<2) return false; // 2 is the first prime

if ((num%2)==0) return num==2; // 2 is the only even prime

unsigned max_div {(unsigned) sqrt (num) + 1};

for (unsigned div=3; div<max_div; div+=2) { // test all odd divisors

if ((num%div)==0) return false; // div is a factor of num, so num is not prime

}

return true; // if we get this far, num has no factors so it is prime

}

int main () {

using namespace std;

unsigned count = 0;

unsigned num = 0;

while (count<10) {

if (is_prime (num)) {

cout << num << endl;

++count;

++num;

}

}

User Avatar

Wiki User

6y ago
User Avatar

To understand how to generate prime numbers, you need to revisit the definition of a Prime number:

A prime number is any x such that x is not a multiple of anything else.

You should also make the observation that is x is a multiple of some y, then y is necessarily less than x.

Now think of it like this.

Say we set all numbers in an array of numbers to be prime numbers.

Also, say 2 is our initial prime number.

Then we know that all multiples of 2 cannot be prime numbers, because it contradicts our above definition. Therefore, 4,6,8,10,... are all crossed off our list.

Going up in order, we now encounter 3. You notice that 3 is not crossed off the list. You can conclude then that there is no y (1

Next we encounter 4, but it is crossed off, so we skip it.

Then we encounter 5,......etc.

The method described above is called the Sieve of Eratosthenes, and is usually very efficient for prime number generation. Note however that this algorithm puts a strain on memory, so you shouldn't try to generate a large set of primes if you're working with a strict memory limit.

In case you're wondering how to implement it, here is my attempt at it (I apologize if PASCAL is inconvenient):

You can run this code on www.ideone.com (choose PASCAL(FPC) as the language)

Copy and paste this for the input:

2

1 10

3 5

And the Code:

  • uses Crt;
  • var
  • n,a,b,r,aa,bb,cc,l:longint;
  • prime:array[0..1000000] of longint;
  • p,po:array[0..10000000] of boolean;
  • begin
  • readln(n);
  • for cc:= 1 to n do begin
  • readln(a,b);
  • for aa:= 1 to round(sqrt(b)) do p[aa]:=false;
  • for aa:= 0 to b-a do po[aa]:=false;
  • prime[1]:=2;
  • l:=1;
  • r:=3;
  • while r <= sqrt(b) do begin
  • if not p[r] then begin
  • inc(l);
  • prime[l]:=r;
  • for aa:= 2 to trunc(sqrt(b)/r) do p[aa*r]:=true;
  • end;
  • inc(r,2);
  • end;
  • for aa:= 1 to l do begin
  • for bb:= trunc(a/prime[aa]) to trunc(b/prime[aa]) do begin
  • if bb*prime[aa] <> prime[aa] then
  • po[bb*prime[aa]-a]:=true;
  • end;
  • end;
  • for aa:= 0 to b-a do begin
  • if aa+a <> 1 then begin
  • if not po[aa] then writeln(aa+a);
  • end;
  • end;
  • writeln;
  • end;
  • readln;
  • end.

You can now try coding one for yourself. Happy Practicing!

User Avatar

Wiki User

13y ago
User Avatar

let count = 0;

let even = 0; // zero is the first even number

while count != 10

{

print even;

even = even + 2;

count = count + 1;

}

If you want to test if an integer is even or odd, test the low-order bit using bitwise AND (&):

if num & 0x1 then

// num is odd

else

// num is even

Note that this is only guaranteed to work when all systems use twos-complement notation. If your program is intended to work on systems that may still be using ones-complement notation (which is rare these days), it's better to use the modus operator (%) instead:

if num % 2 then // num is odd

else

// num is even

The modus operator simply returns the remainder after division. Dividing any integer by 2 always leaves a remainder of either 0 (false, therefore even) or 1 (true, therefore odd) and is guaranteed to work on both ones-complement as well as twos-complement systems. However, the division adds a computational overhead that is unnecessary on a twos-complement system. A better approach would be to use precompiler directives or compile-time computation to determine the actual notation and choose the appropriate operation accordingly.

User Avatar

Wiki User

9y ago
User Avatar

A prime number is distinguished by the fact that it has no natural factors other than one and itself. 0 is not a natural number nor is it a factor of 0 (any division by zero is invalid). Zero also has infinite natural factors since 0/1=0/2=0/3 and so on. Therefore zero is non-prime. One is also non-prime because it only has one natural factor, itself. To be prime it must have two factors, one and itself.


Two is the first prime because its only natural factors are 1 and 2 (one and itself). It therefore follows that all multiples of two are non-prime because they all have two as a natural factor. Two is therefore a prime factor of all even values greater than two because two is both natural and prime. This excludes 4, 6, 8, 10 and so on.


Three is the next prime because its only natural factors are 1 and 3 (one and itself). It therefore follows that all multiples of three are non-prime because they all have three as a natural factor. Three is therefore a prime factor of all multiples of three, which excludes 6, 9, 12, 15 and so on. But 6 and 12 are already excluded by prime factor two so there's no need to test if an even value is divisible by three. We only need to test the odd values.


Four is excluded by prime factor two, so the next prime is five. This then excludes all multiples of five: 10, 15, 20, 25, and so on. But 10 and 20 were excluded by two, and 15 was excluded by three, thus we're only interested in multiples of five that are not multiples of two or three, which includes 25, 35, 55, and so on.


If we continue in this manner we can see that 6 is non-prime (prime factor two), 7 is prime, 8 is non-prime (prime factor two), 9 is non-prime (prime factor three), 10 is non-prime (prime factor two) and 11 is prime, and so on. We can therefore surmise that in order to test if a value is prime or not, we need to know all the prime values that are less than that value, to see if any are factors. We can also see that the maximum possible factor of a value (other than itself) must be the square root of the value. This is because if there were any prime factors higher than the square root, there must also be a prime factor lower than the square root. If there is a lower prime factor, there's no need to keep checking; the value is obviously non-prime. If there is no lower prime factor, there can be no higher prime factor; the value is obviously prime.


If we're only testing a small range of low-order values, we can "cheat" by using the following algorithm:


1. If the value is less than 2 then it is non-prime.

2. If the value is 2 it is prime.

3. If the value has any factors in the range 3 to the square root of the value, it is non-prime, otherwise it is prime.


The problem with this is that it is inefficient. We know that all even values other than 2 are non-prime, so we don't need to test if 4, 6, 8 and so on are factors. Eliminating these factors would cut the number of tests we need to conduct by half. So we can modify the algorithm slightly:


1. If the value is less than 2 then it is non-prime.

2. If the value is even, it is prime if and only if it is 2, otherwise it is non-prime.

3. If the value has any odd factors in the range 3 to the square root of the value, it is non-prime, otherwise it is prime.


While this helps, it is still inefficient because we'd still be testing 9, 15, 21, 27 and so on, all of which are multiples of 3, as well as 25, 35, 55, and 65 which are all multiple of 5. We're only actually interested in the prime factors, so what we need to do is maintain a table of primes. If the value is in the table then it is prime. If not, then we simply test it against all the values that are in the table. If the number turns out to be prime, then we add it to the table.


We know the first primes are 2 and 3 so we initialise the table with those values. Our algorithm then becomes:


1. If the value is less than 2 then it is non-prime. Return false.

2. If the value is in the table, it is prime. Return true.

3. If the value is not in the table, divide by each value in the table. If any division leaves no remainder, the value is not prime. Return false.

4. If the value is not a multiple of any value in the table and the last value in the table is greater than its square root, the value is prime. Add it to the end of the table. Return true.


This then allows us to generate a sequence of primes up to any value, but it does not allow us to determine if any single value is prime unless we previously generated all primes up to its square root. For that we need to add the following steps:


5. Use temporary value (last value + 2).

6. If the temporary value is prime and is a factor of the value, return false.

7. If the temporary value is less than the square root, goto 5.

8. Return true.


Note that in step 6 we re-invoke the algorithm upon the temporary value. If it is prime, it will be added to the table in step 4.

User Avatar

Wiki User

10y ago
User Avatar

loop x from 10 to 20 by 1

loop y from 2 to x-1 by 1

test: if x divisible by y then continue to next loop x iteration

end loop y

print x

end loop x

User Avatar

Wiki User

13y ago
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write algorithm to print first ten even numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp