In this article, we will discuss a C++ program for Centred Nonagonal Number . But before going to its implementation, we must know about Centred Nonagonal Number in C++.
What is the Centred Nonagonal Number?
A number that indicates how many dots are in a centred nonagon is called a Centred Nonagonal Number . It is part of a series of integers produced by the formula, "(3 num - 2) (3 * num - 1) / 2", in which 'num' is a non-negative integer.
Example 1:
Let us take an example to illustrate the Centred Nonagonal Number in C++.
#include <iostream>
// A function that determines the centered nonagonal number
int centered_NonagonalNumber(int num)
{
// "(3 * num - 2) * (3 * num - 1) / 2" is the formula for a centered nonagonal number.
return (3 * num - 2) * (3 * num - 1) / 2;
}
int main()
{
int num;
std::cout << "Enter the value of num to calculate the centered nonagonal number: ";
std::cin >> num;
// Verify that 'num' is non-negative.
if (num < 0)
{
std::cout << "Please enter a non-negative integer.\n";
return 1;
}
// Determine the "centred nonagonal number" and display it.
int p = centered_NonagonalNumber(num);
std::cout << "The centered nonagonal number for num = " << num << " is: " << p << std::endl;
return 0;
}
Output:
Enter the value of num to calculate the centered nonagonal number: 11
The centered nonagonal number for num = 10 is: 496
Explanation:
This program creates a function called centeredNonagonalNumber , which gives an integer 'num' as input, returns the nth centered nonagonal number using the formula "(3 num - 2) (3 * num - 1) / 2". The corresponding centered nonagonal number is subsequently calculated by the main Function, which then asks the user to enter a value for 'num' and displays its result.
Algorithm
- First, take the number 'p' as input.
- Use a loop to iterate from num = 1 to num = 'p' .
- Utilizing the formula ((3 num) - 2) ((3 * num) - 1)) / 2 to determine the centred nonagonal number inside the loop.
- The computed centred nonagonal number will be displayed.
- Repeat steps 3-4 for all values of 'p' from 1 to num.
Example 2:
Let us take another example to illustrate the Centred Nonagonal Number in C++.
#include <iostream>
#include <vector>
//Function to calculate the centered nonagonal number for a given value of num
int centeredNonagonalNumber(int num)
{
return (3 * num - 2) * (3 * num - 1) / 2;
}
int main()
{
int p;
std::cout << "Enter the value of p: ";
std::cin >> p;
if (p <= 0)
{
std::cout << "Please enter a positive integer for p." << std::endl;
return 1;
}
// Vector for storing the centered nonagonal numbers
std::vector<int> centeredNonagonalNumbers;
// Calculating the centered nonagonal numbers from num = 1 to num = p
for (int num = 1; num <= p; ++num)
{
centeredNonagonalNumbers.push_back(centeredNonagonalNumber(num));
}
// Displaying the calculated centered nonagonal numbers
std::cout << "The Centered Nonagonal Numbers from num = 1 to num = " << p << ":" << std::endl;
for (int num = 1; num <= p; ++num)
{
std::cout << "num = " << num << ": " << centeredNonagonalNumbers[num - 1] << std::endl;
}
return 0;
}
Output:
Enter the value of p: 13
The Centered Nonagonal Numbers from num = 1 to num = 13:
num = 1: 1
num = 2: 10
num = 3: 28
num = 4: 55
num = 5: 91
num = 6: 136
num = 7: 190
num = 8: 253
num = 9: 325
num = 10: 406
num = 11: 496
num = 12: 595
num = 13: 703
Explanation:
This program prompts the user to provide the value of 'p' to calculate centred nonagonal numbers. The centred nonagonal numbers from num=1 to num=p are calculated and printed. The program reminds users to provide a positive integer if they enter a non-positive integer for 'p' . If the user inputs a non-positive integer for 'p' , the program prompts them to enter a positive integer.
Applications
Many mathematical contexts allow for the use of centered nonagonal numbers, including:
- Pattern generation and analysis.
- Calculations in geometry using nonagonal shapes.
- The design of algorithms is necessary, particularly when working with polygonal numbers.
- Number theory and cryptography.
Complexity Analysis:
Time Complexity:
The code provided has an O(n) time complexity, where n is the input value. This is because the algorithm computes and displays the computed nominal numbers using an iteration loop that runs n times.
Space Complexity:
In C++, computing a centered nonagonal number has a space complexity of O(1) . This is because the program uses the same amount of memory regardless of the input size.