This discussion is archived
1 2 Previous Next 27 Replies Latest reply: Jun 23, 2007 11:53 PM by 807600 Go to original post
• ###### 15. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
I'm sure it can be done more elegantly than this
but
thats up to you.
Beautiful. Just f'n beautiful.

This teaches the OP how to go sparrow hunting with an
Uzi. Nuthin left but feathers.
afaik its not homework and i purposly didn't made clean code so he can figureout how to do the for loop and whatever he wants to do with it.

i don't see a problem with this.
• ###### 16. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
Biginteger? we don't want to mislead the OP either.
• ###### 17. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
...
//          Ark de Appel                www.engineeringserver.com
//
//----------------------------------------------------
------------------*/
There is no need for such a disclaimer/header because AFAIK, what you just posted is no intellectual property of yours. You only demonstrated the use of some standard Java classes. Nothing more.

import java.math.BigInteger;

class Sun_Algho {
public static void main(String [] args){

int isPrime = 137;
int result = 0;
int i = 1;

System.out.println("Test Prime: ");
for (int j = 0; j < 100; j++){
BigInteger bigInt = BigInteger.valueOf(j);
if(bigInt.isProbablePrime(10)){
Why did you choose the number 10 in isProbablePrime(10)?

...
System.out.println("Test# 2");

if (isPrime%i == 0){

result++;
}
if (result <=2){
System.out.println("Prime: " + isPrime + " is
is Prime? Yes ");
}
else{
System.out.println("Prime: " + isPrime + " is
is Prime? No ");
...
What is this test #2? If you change isPrime to composite number, it'll be marked as a prime number!
• ###### 18. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
...
afaik its not homework and i purposly didn't made
clean code so he can figureout how to do the for loop
and whatever he wants to do with it.
Sticking some lines of code in one (main) method is not helping the OP at all, IMHO.
You could have at least used two different methods and pointed out that the OP needed to do some work with test #2. Just saying "it's not elegant code" doesn't help the OP much with what's wrong with it.

i don't see a problem with this.
I do.
• ###### 19. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
Its a template i use in eclipe for any new class that i make.

Anyway i'll look into that. I probably forgot a check somewhere.
• ###### 20. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
Its a template i use in eclipe for any new class that
i make.

Anyway i'll look into that. I probably forgot a check
somewhere.
Yes, I figured that much. What I mean is you don't need to post it here if you're posting some lines of code.
• ###### 21. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
...
afaik its not homework and i purposly didn't made
clean code so he can figureout how to do the for
loop
and whatever he wants to do with it.
Sticking some lines of code in one (main) method is
not helping the OP at all, IMHO.

i don't see a problem with this.
I do.
Looking at my code again I suppose i could create a method for it and split the code so its easier to read and understand.
• ###### 22. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
Its a template i use in eclipe for any new class
that
i make.

Anyway i'll look into that. I probably forgot a
check
somewhere.
Yes, I figured that much. What I mean is you don't
need to post it here if you're posting some lines of
code.
I totally agree on that. i'll work on my posting skill in my future posts :)

Message was edited by:
deAppel
• ###### 23. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
...
I totally agree on that. i'll work on my posting
skill in my future posts :)
• ###### 24. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
• ###### 25. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
BigInteger.valueOf(j);
if(bigInt.isProbablePrime(10)){
Why did you choose the number 10 in
isProbablePrime(10)?
AFAIK, the bigger the argument in the isProbablyPrime method, the greater the accuracy, but the slower the calculation. But the OP doesn't need obscure approximation methods for calculating primes, the OP needs a precise simple algorithm. I liken the above to sparrow hunting w/ an uzi. Sure he can kill birds with it, but with what result? What useful information does he gain?
• ###### 26. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
...
Why did you choose the number 10 in
isProbablePrime(10)?
AFAIK, the bigger the argument in the isProbablyPrime
method, the greater the accuracy, but the slower the
calculation. ...
Yes, I know. I was more interested in the motivation of the author of that code as to why s/he choose the number 10.
; )

But the OP doesn't need obscure
approximation methods for calculating primes, the OP
needs a precise simple algorithm. I liken the above
to sparrow hunting w/ an uzi. Sure he can kill birds
with it, but with what result? What useful
information does he gain?
Probably none.
• ###### 27. Re: why   the  outprint  for  the  first number in loop only?
Currently Being Moderated
@vanpersie:
to print prime elements between zero and 25
Then my suggestion is the following:
Write a method that has the signature: boolean isPrime(int n)
This method returns true if the number n is a prime and false otherwise.

Use this method in the following code:
``````for (int i = 0; i < 26; i++) {
if (isPrime(i)) {
System.out.println("A prime: " + i);
}
}``````
Much clearer, don't you think? (It also seperates the showing of the result from your 'prime-determining-code' which allows you to focus better.)
1 2 Previous Next