Pentagonal number
In number theory, a pentagonal number is a figurate number that can be represented in the form of a regular pentagon. These numbers are obtained by counting the dots in the successive layers of a regular pentagon. The formula to calculate the nth pentagonal number is given by:
Pn = (3n^2 - n) / 2
Problem Statement
The task at hand is to write a program that calculates and prints the first 'k' pentagonal numbers, where 'k' is a given positive integer. The program should use the above formula to calculate each pentagonal number and then display the result.
Explanation with Suitable Example
Let's take an example with k = 6. The program should calculate and display the first 6 pentagonal numbers.
Pseudocode
function pentagonalNo(k)
// Input: k - number of pentagonal numbers to be calculated
for n = 1 to k do
// Calculate the nth pentagonal number using the formula
result = (3 * n^2 - n) / 2
// Display the calculated pentagonal number
print result
end function
Before diving into the code explanation, let's outline the pseudocode for the given task:
- Create a function
pentagonalNo(k)
to calculate and display the first 'k' pentagonal numbers. - Inside the function, use a loop to iterate from n = 1 to n = k.
- For each 'n', calculate the pentagonal number using the formula:
result = (3 * (n * n) - n) / 2
. - Print the calculated pentagonal number for each value of 'n'.
Code Solution
Here given code implementation process.
// C Program for
// Pentagonal number
#include <stdio.h>
void pentagonalNo(int k)
{
// Print all initial k pentagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
int result = (3 *(n *n) - n) / 2;
// Display calculated result
printf(" %d", result);
}
}
int main()
{
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
pentagonalNo(10);
return 0;
}
Output
1 5 12 22 35 51 70 92 117 145
// Java program for
// Pentagonal number
public class PentagonalNumber
{
public void pentagonalNo(int k)
{
// Print all initial k pentagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
int result = (3 * (n * n) - n) / 2;
// Display calculated result
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
PentagonalNumber task = new PentagonalNumber();
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
task.pentagonalNo(10);
}
}
Output
1 5 12 22 35 51 70 92 117 145
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Pentagonal number
class PentagonalNumber
{
public: void pentagonalNo(int k)
{
// Print all initial k pentagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
int result = (3 *(n *n) - n) / 2;
// Display calculated result
cout << " " << result;
}
}
};
int main()
{
PentagonalNumber *task = new PentagonalNumber();
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
task->pentagonalNo(10);
return 0;
}
Output
1 5 12 22 35 51 70 92 117 145
// Include namespace system
using System;
// Csharp program for
// Pentagonal number
public class PentagonalNumber
{
public void pentagonalNo(int k)
{
// Print all initial k pentagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
int result = (3 * (n * n) - n) / 2;
// Display calculated result
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
PentagonalNumber task = new PentagonalNumber();
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
task.pentagonalNo(10);
}
}
Output
1 5 12 22 35 51 70 92 117 145
package main
import "fmt"
// Go program for
// Pentagonal number
func pentagonalNo(k int) {
// Print all initial k pentagonal number
for n := 1 ; n <= k ; n++ {
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
var result int = (3 * (n * n) - n) / 2
// Display calculated result
fmt.Print(" ", result)
}
}
func main() {
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
pentagonalNo(10)
}
Output
1 5 12 22 35 51 70 92 117 145
<?php
// Php program for
// Pentagonal number
class PentagonalNumber
{
public function pentagonalNo($k)
{
// Print all initial k pentagonal number
for ($n = 1; $n <= $k; ++$n)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
$result = (int)((3 * ($n * $n) - $n) / 2);
// Display calculated result
echo(" ".$result);
}
}
}
function main()
{
$task = new PentagonalNumber();
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
$task->pentagonalNo(10);
}
main();
Output
1 5 12 22 35 51 70 92 117 145
// Node JS program for
// Pentagonal number
class PentagonalNumber
{
pentagonalNo(k)
{
// Print all initial k pentagonal number
for (var n = 1; n <= k; ++n)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
var result = parseInt((3 * (n * n) - n) / 2);
// Display calculated result
process.stdout.write(" " + result);
}
}
}
function main()
{
var task = new PentagonalNumber();
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
task.pentagonalNo(10);
}
main();
Output
1 5 12 22 35 51 70 92 117 145
# Python 3 program for
# Pentagonal number
class PentagonalNumber :
def pentagonalNo(self, k) :
n = 1
# Print all initial k pentagonal number
while (n <= k) :
# Formula
# (3n²-n)
# —————————
# 2
# Calculate nth pentagonal number
result = int((3 * (n * n) - n) / 2)
# Display calculated result
print(" ", result, end = "")
n += 1
def main() :
task = PentagonalNumber()
# Pentagonal number are
# —————————————————————————————————————————————
# 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
# 210, 247, 287, 330, 376, 425, 477, 532, 590,
# 651, 715, 782, 852, 925, 1001, 1080, 1162,
# 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
# 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
# k = 10
task.pentagonalNo(10)
if __name__ == "__main__": main()
Output
1 5 12 22 35 51 70 92 117 145
# Ruby program for
# Pentagonal number
class PentagonalNumber
def pentagonalNo(k)
n = 1
# Print all initial k pentagonal number
while (n <= k)
# Formula
# (3n²-n)
# —————————
# 2
# Calculate nth pentagonal number
result = (3 * (n * n) - n) / 2
# Display calculated result
print(" ", result)
n += 1
end
end
end
def main()
task = PentagonalNumber.new()
# Pentagonal number are
# —————————————————————————————————————————————
# 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
# 210, 247, 287, 330, 376, 425, 477, 532, 590,
# 651, 715, 782, 852, 925, 1001, 1080, 1162,
# 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
# 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
# k = 10
task.pentagonalNo(10)
end
main()
Output
1 5 12 22 35 51 70 92 117 145
// Scala program for
// Pentagonal number
class PentagonalNumber()
{
def pentagonalNo(k: Int): Unit = {
var n: Int = 1;
// Print all initial k pentagonal number
while (n <= k)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
var result: Int = (3 * (n * n) - n) / 2;
// Display calculated result
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: PentagonalNumber = new PentagonalNumber();
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
task.pentagonalNo(10);
}
}
Output
1 5 12 22 35 51 70 92 117 145
// Swift 4 program for
// Pentagonal number
class PentagonalNumber
{
func pentagonalNo(_ k: Int)
{
var n: Int = 1;
// Print all initial k pentagonal number
while (n <= k)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
let result: Int = (3 * (n * n) - n) / 2;
// Display calculated result
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
let task: PentagonalNumber = PentagonalNumber();
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
task.pentagonalNo(10);
}
main();
Output
1 5 12 22 35 51 70 92 117 145
// Kotlin program for
// Pentagonal number
class PentagonalNumber
{
fun pentagonalNo(k: Int): Unit
{
var n: Int = 1;
// Print all initial k pentagonal number
while (n <= k)
{
// Formula
// (3n²-n)
// —————————
// 2
// Calculate nth pentagonal number
val result: Int = (3 * (n * n) - n) / 2;
// Display calculated result
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: PentagonalNumber = PentagonalNumber();
// Pentagonal number are
// —————————————————————————————————————————————
// 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176,
// 210, 247, 287, 330, 376, 425, 477, 532, 590,
// 651, 715, 782, 852, 925, 1001, 1080, 1162,
// 1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926,
// 2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
// k = 10
task.pentagonalNo(10);
}
Output
1 5 12 22 35 51 70 92 117 145
Output Explanation
The program is executed with 'k' as 10, so it will calculate the first 10 pentagonal numbers and print the results. The output will be:
1 5 12 22 35 51 70 92 117 145
Each number in the output represents the first 10 pentagonal numbers.
Time Complexity Analysis
In the given program, the function pentagonalNo(k)
runs a loop from 1 to 'k' to calculate 'k' pentagonal
numbers. For each value of 'n', the calculation involves basic arithmetic operations, which can be considered
constant time operations.
Therefore, the time complexity of the program is O(k), where 'k' is the number of pentagonal numbers to be calculated.
Finally
In this program, we have successfully calculated the first 'k' pentagonal numbers using the given formula. We've explained the code step by step, provided suitable examples, and outlined the time complexity. Pentagonal numbers have applications in various areas, including number theory, geometry, and combinatorics. The program can be extended or used as a part of a larger application where pentagonal numbers are required.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment