Math: pentagonal numbers

• 10-09-2010, 10:38 PM
Latanyar
Math: pentagonal numbers
I am writing a method that returns a pentagonal number.

public static int getPentagonalNumber(int n)

I have to write a test program that displays the first 100 pentagonal numbers with 10 numbers on each line.

This is what I have so far.

public class getPentagonalNumber
{
public static void main(String[] args)
{
int n;
int getPentagonalNumber = ((n) *(3n - 1)(/2));
for(n=1; n >=100; n++)
{
System.out.println(n(1, 100));
System.out.println();
}
}
public static int getPentagonalNumber(int n)
{
return getPentagonalNumber;

}
}

I'm not sure if this is the correct format on starting the method. Could anyone guide me into the right direction?
• 10-09-2010, 10:49 PM
eRaaaa
uiuiui, no no..
you have to move the formula in the method.(the formula has some syntax error e.g. 3n)
in your loop you have to call this method with i as the paramter. and you must print a linebreak if "i" modulo 10 is equal 0

(and rename your class :D )
• 10-10-2010, 12:05 AM
Latanyar
Quote:

Originally Posted by eRaaaa
uiuiui, no no..
you have to move the formula in the method.(the formula has some syntax error e.g. 3n)
in your loop you have to call this method with i as the paramter. and you must print a linebreak if "i" modulo 10 is equal 0

(and rename your class :D )

public class getPentagonalNumber
{
public static void main(String[] args)
{
int n;

for(n=1; n >=100; n++)
{
System.out.println(n(1, 100));
System.out.println();
}
}
public static int getPentagonalNumber(int n)
{
int getPentagonalNumber = n *(3*n - 1)/2;
return getPentagonalNumber;

}
}

Ok. I changed (3n to (3*n-1)/2 but System.out.println(n(1, 100));
is saying cannot find symbol method. What should I rename my class?
• 10-10-2010, 12:13 AM
eRaaaa
....
first: for(n=1; n >=100; n++) - you will never enter the loop...--> n<=100
second: System.out.println(n(1, 100)); - :confused: i dont know what are you trying there :) - call the method !!

Code:

```public class PentagonalNumber {         public static void main(String[] args) {                 for (int i = 1; i <= 100; i++) {                         //System.out.print(getPentagonalNumber(i) + (i %10 == 0? System.getProperty("line.separator") : "\t")); //advanced ;D                         System.out.print(getPentagonalNumber(i));                         if (i % 10 == 0) {                                 System.out.println();                         } else {                                 System.out.print("\t");                         }                 }         }         public static int getPentagonalNumber(int n) {                 return (3 * n * n - n) / 2;         } }```
• 10-10-2010, 01:59 AM
Latanyar
Thank you, but I am still confused at the point where

if (i % 10 == 0) {
System.out.println();
} else {
System.out.print("\t");
}
}
}
I guess I am trying to grasp the concept on methods. We just started on methods and I am really confused. Also, could I use n instead of i, because it says n*(3*n-1)/2 for n = 1, 2....and so on. So could mines get put in this way:

public class getPentagonalNumber
{
public static void main(String[] args)
{
int n;

for(n=1; n <=100; n++)
{
System.out.print(getPentagonalNumber(n));
if (n % 10 == 0)
{
System.out.println();
}
else
{
System.out.print("\t");
}
}
}

public static int getPentagonalNumber(int n)
{

return n *(3*n - 1)/2;

}
}
• 10-10-2010, 02:02 AM
al_Marshy_1981
Quote:

Originally Posted by Latanyar
I am writing a method that returns a pentagonal number.

public static int getPentagonalNumber(int n)

I have to write a test program that displays the first 100 pentagonal numbers with 10 numbers on each line.

This is what I have so far.

public class getPentagonalNumber
{
public static void main(String[] args)
{
int n;
int getPentagonalNumber = ((n) *(3n - 1)(/2));
for(n=1; n >=100; n++)
{
System.out.println(n(1, 100));
System.out.println();
}
}
public static int getPentagonalNumber(int n)
{
return getPentagonalNumber;

}
}

I'm not sure if this is the correct format on starting the method. Could anyone guide me into the right direction?

int getPentagonalNumber = ((n) *(3n - 1)(/2));

n has not been assigned and you cannot do 3n, it means nothing to java.
• 10-10-2010, 06:32 AM
Zack
Quote:

Originally Posted by eRaaaa
....
first: for(n=1; n >=100; n++) - you will never enter the loop...--> n<=100
second: System.out.println(n(1, 100)); - :confused: i dont know what are you trying there :) - call the method !!

Code:

`...`

Please avoid spoon-feeding the OP as he will learn nothing this way. Simply providing a solution is unhelpful and does more harm than good, as when he comes across a test environment he won't have that kind of help.

@ OP, that part of the code that you don't get puts ten numbers on each line. The print() function prints out text without a newline, then the println() function prints out that text WITH a newline, as such:
Code:

```System.out.println("a"); System.out.print("b"); System.out.print("c"); System.out.println("d"); System.out.print("e"); System.out.println(); System.out.print("f");```
This code will produce:
Code:

```abcd e f```
Play around with both print() and println() to see what you can do.

Also, if you want to understand the point of the n%10 part, look up some stuff on the modulus operator: The Remainder or Modulus Operator in Java

Good luck!
• 10-10-2010, 03:14 PM
Latanyar
Thank you.