In this tutorial, we will explore a C++ code for generating Centred Nonagonal Numbers. Prior to delving into the code, it is essential to understand the concept of Centred Nonagonal Numbers in C++.
What is the Centred Nonagonal Number?
A value denoting the quantity of dots in a centrally positioned nonagon is referred to as a Centred Nonagonal Number. This numerical value is part of a sequence of whole numbers generated using the expression, "(3 num - 2) (3 * num - 1) / 2", where 'num' represents a non-negative integer.
Example 1:
Let's consider a scenario to demonstrate the Centred Nonagonal Number concept using 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 script defines a function named centeredNonagonalNumber. It accepts an integer 'num' as a parameter and calculates the nth centered nonagonal number using the formula "(3 num - 2) (3 * num - 1) / 2". The main function invokes this method to compute the centered nonagonal number based on the user-provided 'num' value, displaying the result afterwards.
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's consider another instance to demonstrate the Centred Nonagonal Number concept in the C++ programming language.
#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 software requests the user to input the value of 'p' in order to compute centred nonagonal figures. It computes and displays the centred nonagonal numbers ranging from num=1 to num=p. Additionally, it offers a prompt to ensure users input a positive integer if a non-positive integer is provided for 'p'. In case the user enters a non-positive integer for 'p', the software guides them to input a positive integer instead.
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 provided code operates with an O(n) time complexity, where n represents the input value. This is due to the fact that the program calculates and exhibits the calculated nominal figures through an iterative loop that iterates n times.
Space Complexity:
In C++, when computing a centered nonagonal number, the space complexity remains constant at O(1). This is due to the program utilizing a consistent amount of memory irrespective of the input magnitude.