Thread: Math: pentagonal numbers

1. Member Join Date
Oct 2010
Posts
25
Rep Power
0 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?  Reply With Quote

2. Senior Member  Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
11 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 )
Last edited by eRaaaa; 10-09-2010 at 10:52 PM. Reason: 10 numbers on each line not 5, sorry :)  Reply With Quote

3. Member Join Date
Oct 2010
Posts
25
Rep Power
0  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?  Reply With Quote

4. Senior Member  Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
11 ....
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 !!

Java 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;
}
}  Reply With Quote

5. Member Join Date
Oct 2010
Posts
25
Rep Power
0 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;

}
}  Reply With Quote

6. Senior Member Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
10  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.  Reply With Quote

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

Java 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:
Java 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:
Java 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!  Reply With Quote

8. Member Join Date
Oct 2010
Posts
25
Rep Power
0 Thank you.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•