Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi everyone,
I should find prime numbers! its so easy!
but the point is how the program will looks when you cannot use arithmetic operators!!
(except '++' to increment)
best regards,
A. Sojoodi

This sounds like a very interesting problem. Is it some sort of computer science quiz? What language are we talking about? Perhaps you can use the concept of symbols "Zero" and function "Succ" that are often studied in the context of functional and logic languages?

Hi klint,
This program should be written in java.
there is a solution and it is sieve algorithm.
but its not object oriented.
What should we do?!

Eratosthenes' Sieve involves going through a list of all the integers and removing all multiples of each successive prime number which is found at each successive element of the list. Are you sure you can't use any arithmetic operators other than ++? It is slightly cumbersome to remove all multiples of a number without using +.

Finally I find the answer!
for example finding prime numbers upto 6:public class Prime2 {
static int k = 2;
static int[] mod = { 0,0,0,0,0,0 };
static int[] prime = { 0,0,0,0,0,0 };
static int max = 6;
static boolean isPrime;
public static void main(String[] args) {
for (int n = 0; n < max; k++) {
isPrime = true;
for (int i = 0; i < n; i++) {
mod[i]--;
if (mod[i] == 0) {
mod[i] = prime[i];
isPrime = false;
}
}
if (isPrime) {
System.out.println(k);
prime[n] = k;
mod[n] = k;
n++;}
}
}
}

That's interesting. Thanks for sharing it. Unfortunately it's a bit hard to follow and understand how it works.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |