Computing.Net > Forums > Programming > Prime numbers in oop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Prime numbers in oop

Reply to Message Icon

Name: AmirHossein
Date: November 15, 2008 at 04:08:04 Pacific
OS: vista bu
CPU/Ram: 2.4g 3m 3g
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: November 17, 2008 at 02:01:04 Pacific
Reply:

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?


0

Response Number 2
Name: AmirHossein
Date: November 18, 2008 at 04:09:01 Pacific
Reply:

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?!


0

Response Number 3
Name: klint
Date: November 19, 2008 at 02:20:02 Pacific
Reply:

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 +.


0

Response Number 4
Name: AmirHossein
Date: November 19, 2008 at 08:17:15 Pacific
Reply:

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

}
}
}
}


0

Response Number 5
Name: klint
Date: November 20, 2008 at 09:33:00 Pacific
Reply:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Prime numbers in oop

Find prime numbers in Java? www.computing.net/answers/programming/find-prime-numbers-in-java/11287.html

Prime Number in MIPS help www.computing.net/answers/programming/prime-number-in-mips-help/13454.html

Prime number checking www.computing.net/answers/programming/prime-number-checking/6158.html