Please Ignore

NVM I figured it out.. doh!Message was edited by: Overkill
[79 byte] By [Overkilla] at [2007-11-27 8:39:51]
# 1

Ok, I'm not going to look at your code: you should really split things into two separate methods. Here's some pseudo code:

public class TwinPrimes {

public void displayTwinPrimes(int low, int high) {

/*

LOOP 'i' from 'low' to 'high'

IF 'i' is a prime

IF 'i' is even

print '2,3'

ELSE IF 'i'+2 is prime

print 'i' and 'i'+2

END IF

END IF

END LOOP

*/

}

public boolean isPrime(int n) {

// return true iff 'n' is prime

}

public static void main(String[] args) {

new TwinPrimes().displayTwinPrimes(1,100);

}

}

Message was edited by:

prometheuzz

Although you figured it out, I suggest rewriting your application as I suggested: the sooner you learn yourself to split multiple responsibilities into their own methods, the better!

prometheuzza at 2007-7-12 20:38:01 > top of Java-index,Java Essentials,Java Programming...
# 2

Its good you posted that , since right now ive come up with this code, which only prints 1,3

Im taking a look at your code, and after understanding it will write it properly. Thanks

public class TwinPrimeNum//Calculate twin primes from 1-100

{

private int a,b;

public void calculate()

{

for (int i=1;i<=100;i++)

{

for (int j=1;j<=i;j++)

{

if (i%j==0)

{

a++;

}

if ((i+2)%j==0)

{

b++;

}

}

if (a<=2 && b<=2)

System.out.print(i+","+(i+2));

}

}

}

Overkilla at 2007-7-12 20:38:01 > top of Java-index,Java Essentials,Java Programming...
# 3

So i finished translating your pseudo code into BlueJ code(and I removed the low and high things, made it so its only 1 to 100), and it works in a very weird way .. the output it somewhat correct...

This is the output:

1,12

2,3

3,32

5,52

11,112

17,172

29,292

41,412

59,592

71,712

and Twin primes upto 100 are :

(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73), (101, 103)

and this is the code

public class TwinPrimeNumber

{

private int a;

public void display()

{

for (int i=1;i<=100;i++)

{

if (isPrime(i)==true)

{

if (i%2==0) System.out.println("2,3");

else if (isPrime(i+2)==true)

{

System.out.println(i+","+i+2);

}

}

}

}

public boolean isPrime(int n)

{

a=0;

for (int i=1;i<=n;i++)

{

if (n%i==0)

a++;

}

if (a<=2)

return true;

else

return false;

}

}

BTW i added a=0 at the top of the isPrime method coz otherwise after the first number , a will always be greater than 2 and nothing will be prime

Message was edited by:

Overkill

Overkilla at 2007-7-12 20:38:01 > top of Java-index,Java Essentials,Java Programming...
# 4

> So i finished translating your pseudo code into BlueJ

> code(and I removed the low and high things, made it

> so its only 1 to 100), and it works in a very weird

> way .. the output it somewhat correct...

Almost!

Instead of doing:

System.out.println(i+","+i+2);

do it like this

System.out.println(i+","+(i+2));

And the number one is not a prime number.

prometheuzza at 2007-7-12 20:38:01 > top of Java-index,Java Essentials,Java Programming...
# 5

Woah!

**** so the +i+2 was making the second + act like a concatenation operator instead of adding...

dooh!

Cool man thanks not only for this program but for introducing me to the basic concept of Object Oriented Programming(using seperate methods IS oop right?). It makes the code so much easier to understand.

Overkilla at 2007-7-12 20:38:01 > top of Java-index,Java Essentials,Java Programming...
# 6

> Woah!

> **** so the +i+2 was making the second + act like a

> concatenation operator instead of adding...

> dooh!

Yeah, that's a classic one.

; )

> Cool man thanks not only for this program but for

> introducing me to the basic concept of Object

> Oriented Programming(using seperate methods IS oop

> right?). It makes the code so much easier to

> understand.

Correct, that's (a part of) OO programming indeed.

Just one small remark about you current isPrime(...) method:-

- that int a should be only visible inside your isPrime(...) method;

- you need not check a number n up to itself for primality: up to the square root is sufficient. If you want to check if 31 is a prime, you can stop the loop when you have reached 6;

So, here's some pseudo code for the isPrime(...) method:

public boolean isPrime(int n) {

/*

IF 'n' is less than 2

'n' is no prime, return false

ELSE IF 'n' equals 2

'n' is prime, return true

END IF

'max' <- (square root of 'n') + 1

FOR 'i' = 2 to 'max'

IF 'i' divides 'n'

'n' is no prime, return false

END IF

END FOR

'n' must be prime if we get here, return true

*/

}

Now you can go ahead with your prime-research and wrap up your proof that there are an infinite number of twin primes... Or aren't there?

; )

prometheuzza at 2007-7-12 20:38:01 > top of Java-index,Java Essentials,Java Programming...
# 7

> Now you can go ahead with your prime-research and

> wrap up your proof that there are an infinite number

> of twin primes... Or aren't there?

> ; )

And when you're done with that, you can move on to these:

http://en.wikipedia.org/wiki/Hilbert%27s_problems

petes1234a at 2007-7-12 20:38:01 > top of Java-index,Java Essentials,Java Programming...