Find the nth icosikaipentagon number
The concept of polygonal numbers involves representing integers as dots arranged in the shape of polygons. An icosikaipentagon number is a type of polygonal number that can be represented using an arrangement of dots in the shape of a 25-gon. The nth icosikaipentagon number is determined by a formula that relates it to the value of n.
Problem Statement
Given a positive integer n, the task is to find the nth icosikaipentagon number using the formula:
Icosikaipentagon(n) = (23 * n^2 - 21 * n) / 2
Example Scenario
Imagine you are working on a mathematical project that involves studying polygonal numbers. You are interested in icosikaipentagon numbers and want to find the 5th and 7th icosikaipentagon numbers using the given formula.
Idea to Solve the Problem
To solve this problem, you can follow these steps:
- Accept a positive integer n as input.
- Use the provided formula to calculate the nth icosikaipentagon number.
- Display the calculated icosikaipentagon number.
Pseudocode
function nthIcosikaipentagonNo(n):
result = (23 * n * n - 21 * n) / 2
return result
main:
n1 = 5
n2 = 7
result1 = nthIcosikaipentagonNo(n1)
result2 = nthIcosikaipentagonNo(n2)
print("Given n :", n1)
print("Result :", result1)
print("Given n :", n2)
print("Result :", result2)
Algorithm Explanation
- Define a function
nthIcosikaipentagonNo
that takes a positive integer n as input. - Use the provided formula to calculate the nth icosikaipentagon number and return the result.
- In the
main
function, set two test cases with different values of n. - Calculate the nth icosikaipentagon number for each test case by calling the
nthIcosikaipentagonNo
function. - Display the calculated icosikaipentagon numbers along with their respective values of n.
Code Solution
// C Program
// Find the nth icosikaipentagon number
#include <stdio.h>
void nthIcosikaipentagonNo(int n)
{
// Formula = (23n²-21n)/2
int result = (23 * n * n - 21 * n) / 2;
printf("\n Given n : %d", n);
// Display calculated value
printf("\n Result : %d", result);
}
int main()
{
// Test A
// n = 5
nthIcosikaipentagonNo(5);
// Test B
// n = 7
nthIcosikaipentagonNo(7);
return 0;
}
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
// Java Program
// Find the nth icosikaipentagon number
public class IcosikaipentagonNo
{
public void nthIcosikaipentagonNo(int n)
{
// Formula = (23n²-21n)/2
int result = (23 * n * n - 21 * n) / 2;
System.out.print("\n Given n : " + n);
// Display calculated value
System.out.print("\n Result : " + result);
}
public static void main(String args[])
{
IcosikaipentagonNo task = new IcosikaipentagonNo();
// Test A
// n = 5
task.nthIcosikaipentagonNo(5);
// Test B
// n = 7
task.nthIcosikaipentagonNo(7);
}
}
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Find the nth icosikaipentagon number
class IcosikaipentagonNo
{
public: void nthIcosikaipentagonNo(int n)
{
// Formula = (23n²-21n)/2
int result = (23 *n *n - 21 *n) / 2;
cout << "\n Given n : " << n;
// Display calculated value
cout << "\n Result : " << result;
}
};
int main()
{
IcosikaipentagonNo *task = new IcosikaipentagonNo();
// Test A
// n = 5
task->nthIcosikaipentagonNo(5);
// Test B
// n = 7
task->nthIcosikaipentagonNo(7);
return 0;
}
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
// Include namespace system
using System;
// Csharp Program
// Find the nth icosikaipentagon number
public class IcosikaipentagonNo
{
public void nthIcosikaipentagonNo(int n)
{
// Formula = (23n²-21n)/2
int result = (23 * n * n - 21 * n) / 2;
Console.Write("\n Given n : " + n);
// Display calculated value
Console.Write("\n Result : " + result);
}
public static void Main(String[] args)
{
IcosikaipentagonNo task = new IcosikaipentagonNo();
// Test A
// n = 5
task.nthIcosikaipentagonNo(5);
// Test B
// n = 7
task.nthIcosikaipentagonNo(7);
}
}
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
package main
import "fmt"
// Go Program
// Find the nth icosikaipentagon number
type IcosikaipentagonNo struct {}
func getIcosikaipentagonNo() * IcosikaipentagonNo {
var me *IcosikaipentagonNo = &IcosikaipentagonNo {}
return me
}
func(this IcosikaipentagonNo) nthIcosikaipentagonNo(n int) {
// Formula = (23n²-21n)/2
var result int = (23 * n * n - 21 * n) / 2
fmt.Print("\n Given n : ", n)
// Display calculated value
fmt.Print("\n Result : ", result)
}
func main() {
var task * IcosikaipentagonNo = getIcosikaipentagonNo()
// Test A
// n = 5
task.nthIcosikaipentagonNo(5)
// Test B
// n = 7
task.nthIcosikaipentagonNo(7)
}
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
<?php
// Php Program
// Find the nth icosikaipentagon number
class IcosikaipentagonNo
{
public function nthIcosikaipentagonNo($n)
{
// Formula = (23n²-21n)/2
$result = (int)((23 * $n * $n - 21 * $n) / 2);
echo("\n Given n : ".$n);
// Display calculated value
echo("\n Result : ".$result);
}
}
function main()
{
$task = new IcosikaipentagonNo();
// Test A
// n = 5
$task->nthIcosikaipentagonNo(5);
// Test B
// n = 7
$task->nthIcosikaipentagonNo(7);
}
main();
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
// Node JS Program
// Find the nth icosikaipentagon number
class IcosikaipentagonNo
{
nthIcosikaipentagonNo(n)
{
// Formula = (23n²-21n)/2
var result = parseInt((23 * n * n - 21 * n) / 2);
process.stdout.write("\n Given n : " + n);
// Display calculated value
process.stdout.write("\n Result : " + result);
}
}
function main()
{
var task = new IcosikaipentagonNo();
// Test A
// n = 5
task.nthIcosikaipentagonNo(5);
// Test B
// n = 7
task.nthIcosikaipentagonNo(7);
}
main();
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
# Python 3 Program
# Find the nth icosikaipentagon number
class IcosikaipentagonNo :
def nthIcosikaipentagonNo(self, n) :
# Formula = (23n²-21n)/2
result = int((23 * n * n - 21 * n) / 2)
print("\n Given n : ", n, end = "")
# Display calculated value
print("\n Result : ", result, end = "")
def main() :
task = IcosikaipentagonNo()
# Test A
# n = 5
task.nthIcosikaipentagonNo(5)
# Test B
# n = 7
task.nthIcosikaipentagonNo(7)
if __name__ == "__main__": main()
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
# Ruby Program
# Find the nth icosikaipentagon number
class IcosikaipentagonNo
def nthIcosikaipentagonNo(n)
# Formula = (23n²-21n)/2
result = (23 * n * n - 21 * n) / 2
print("\n Given n : ", n)
# Display calculated value
print("\n Result : ", result)
end
end
def main()
task = IcosikaipentagonNo.new()
# Test A
# n = 5
task.nthIcosikaipentagonNo(5)
# Test B
# n = 7
task.nthIcosikaipentagonNo(7)
end
main()
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
// Scala Program
// Find the nth icosikaipentagon number
class IcosikaipentagonNo()
{
def nthIcosikaipentagonNo(n: Int): Unit = {
// Formula = (23n²-21n)/2
var result: Int = (23 * n * n - 21 * n) / 2;
print("\n Given n : " + n);
// Display calculated value
print("\n Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: IcosikaipentagonNo = new IcosikaipentagonNo();
// Test A
// n = 5
task.nthIcosikaipentagonNo(5);
// Test B
// n = 7
task.nthIcosikaipentagonNo(7);
}
}
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
// Swift 4 Program
// Find the nth icosikaipentagon number
class IcosikaipentagonNo
{
func nthIcosikaipentagonNo(_ n: Int)
{
// Formula = (23n²-21n)/2
let result: Int = (23 * n * n - 21 * n) / 2;
print("\n Given n : ", n, terminator: "");
// Display calculated value
print("\n Result : ", result, terminator: "");
}
}
func main()
{
let task: IcosikaipentagonNo = IcosikaipentagonNo();
// Test A
// n = 5
task.nthIcosikaipentagonNo(5);
// Test B
// n = 7
task.nthIcosikaipentagonNo(7);
}
main();
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
// Kotlin Program
// Find the nth icosikaipentagon number
class IcosikaipentagonNo
{
fun nthIcosikaipentagonNo(n: Int): Unit
{
// Formula = (23n²-21n)/2
val result: Int = (23 * n * n - 21 * n) / 2;
print("\n Given n : " + n);
// Display calculated value
print("\n Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: IcosikaipentagonNo = IcosikaipentagonNo();
// Test A
// n = 5
task.nthIcosikaipentagonNo(5);
// Test B
// n = 7
task.nthIcosikaipentagonNo(7);
}
Output
Given n : 5
Result : 235
Given n : 7
Result : 490
Output Explanation
The code calculates the nth icosikaipentagon number for each test case using the formula and displays the results. For n = 5, the 5th icosikaipentagon number is 235. For n = 7, the 7th icosikaipentagon number is 490.
Time Complexity
The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations that are performed in constant time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.
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