Skip to main content

Octagonal number

Octagonal numbers are a type of figurate numbers that can be visualized as dots arranged in the shape of an octagon. Each octagonal number represents the number of dots that can be used to form an octagon with a particular number of sides. They are a subset of polygonal numbers, which are numbers that can be represented as dots arranged in regular polygons. Octagonal numbers have a pattern that can be described mathematically, making them an interesting subject for exploration.

Problem Statement

The task is to write a C program to print the first 'k' octagonal numbers, where 'k' is a given input. The octagonal numbers can be calculated using the formula: 3n^2 - 2n, where 'n' is the position of the octagonal number in the sequence (1-indexed).

Example

Let's take an example to better understand the problem. Suppose 'k' is given as 10. We need to find the first 10 octagonal numbers.

Algorithm

  1. Start with the given value of 'k', which represents the number of octagonal numbers to be printed.

  2. Create a function called 'octagonalNo' that takes 'k' as an argument.

  3. Inside the 'octagonalNo' function, use a loop to iterate from 'n' = 1 to 'n' = 'k'.

  4. For each value of 'n', calculate the nth octagonal number using the formula: 'result = 3n^2 - 2n'.

  5. Print the calculated octagonal number 'result'.

  6. Repeat steps 4-5 for all values of 'n' from 1 to 'k'.

Pseudocode

Function octagonalNo(k):
    for n from 1 to k:
        result = 3 * n^2 - 2 * n
        print result

Code Solution

Here given code implementation process.

// C Program for
// Octagonal number
#include <stdio.h>

void octagonalNo(int k)
{
	// Print all initial k octagonal number
	for (int n = 1; n <= k; ++n)
	{
		// Formula
		//  (3n²-2n)
      
		// Calculate nth octagonal number
		int result = (3 *(n *n) - 2 *n);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
    // 4256, 4485, 4720 etc.
	// k = 10
	octagonalNo(10);
	return 0;
}

Output

  1  8  21  40  65  96  133  176  225  280
// Java program for
// Octagonal number
public class OctagonalNumber
{
	public void octagonalNo(int k)
	{
		// Print all initial k octagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			int result = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		OctagonalNumber task = new OctagonalNumber();
		//  Octagonal number are
		// —————————————————————————————————————————————
		//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
		// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
		// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
		// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
		// 4256, 4485, 4720 etc.
      
		// k = 10
		task.octagonalNo(10);
	}
}

Output

 1 8 21 40 65 96 133 176 225 280
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Octagonal number

class OctagonalNumber
{
	public: void octagonalNo(int k)
	{
		// Print all initial k octagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			int result = (3 *(n *n) - 2 *n);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	OctagonalNumber *task = new OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
  
	// k = 10
	task->octagonalNo(10);
	return 0;
}

Output

 1 8 21 40 65 96 133 176 225 280
// Include namespace system
using System;
// Csharp program for
// Octagonal number
public class OctagonalNumber
{
	public void octagonalNo(int k)
	{
		// Print all initial k octagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			int result = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		OctagonalNumber task = new OctagonalNumber();
		//  Octagonal number are
		// —————————————————————————————————————————————
		//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
		// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
		// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
		// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
		// 4256, 4485, 4720 etc.
      
		// k = 10
		task.octagonalNo(10);
	}
}

Output

 1 8 21 40 65 96 133 176 225 280
package main
import "fmt"
// Go program for
// Octagonal number

func octagonalNo(k int) {
	// Print all initial k octagonal number
	for n := 1 ; n <= k ; n++ {
		// Formula
		// (3n²-2n)
		// Calculate nth octagonal number
		var result int = (3 * (n * n) - 2 * n)
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.

	// k = 10
	octagonalNo(10)
}

Output

 1 8 21 40 65 96 133 176 225 280
<?php
// Php program for
// Octagonal number
class OctagonalNumber
{
	public	function octagonalNo($k)
	{
		// Print all initial k octagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Formula
			// (3n²-2n)
			// Calculate nth octagonal number
			$result = (3 * ($n * $n) - 2 * $n);
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
	// k = 10
	$task->octagonalNo(10);
}
main();

Output

 1 8 21 40 65 96 133 176 225 280
// Node JS program for
// Octagonal number
class OctagonalNumber
{
	octagonalNo(k)
	{
		// Print all initial k octagonal number
		for (var n = 1; n <= k; ++n)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			var result = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
	// k = 10
	task.octagonalNo(10);
}
main();

Output

 1 8 21 40 65 96 133 176 225 280
#  Python 3 program for
#  Octagonal number
class OctagonalNumber :
	def octagonalNo(self, k) :
		n = 1
		#  Print all initial k octagonal number
		while (n <= k) :
			#  Formula
			#  (3n²-2n)
            
			#  Calculate nth octagonal number
			result = (3 * (n * n) - 2 * n)

			#  Display calculated result
			print(" ", result, end = "")
			n += 1
		
	

def main() :
	task = OctagonalNumber()
	#   Octagonal number are
	#  —————————————————————————————————————————————
	#   1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	#  341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	#  1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	#  2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	#  4256, 4485, 4720 etc.
	#  k = 10
	task.octagonalNo(10)

if __name__ == "__main__": main()

Output

  1  8  21  40  65  96  133  176  225  280
#  Ruby program for
#  Octagonal number
class OctagonalNumber 
	def octagonalNo(k) 
		n = 1
		#  Print all initial k octagonal number
		while (n <= k) 
			#  Formula
			#  (3n²-2n)
            
			#  Calculate nth octagonal number
			result = (3 * (n * n) - 2 * n)

			#  Display calculated result
			print(" ", result)
			n += 1
		end

	end

end

def main() 
	task = OctagonalNumber.new()
	#   Octagonal number are
	#  —————————————————————————————————————————————
	#   1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	#  341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	#  1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	#  2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	#  4256, 4485, 4720 etc.
    
	#  k = 10
	task.octagonalNo(10)
end

main()

Output

 1 8 21 40 65 96 133 176 225 280
// Scala program for
// Octagonal number
class OctagonalNumber()
{
	def octagonalNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k octagonal number
		while (n <= k)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			var result: Int = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: OctagonalNumber = new OctagonalNumber();
		//  Octagonal number are
		// —————————————————————————————————————————————
		//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
		// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
		// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
		// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
		// 4256, 4485, 4720 etc.
  
		// k = 10
		task.octagonalNo(10);
	}
}

Output

 1 8 21 40 65 96 133 176 225 280
// Swift 4 program for
// Octagonal number
class OctagonalNumber
{
	func octagonalNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k octagonal number
		while (n <= k)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			let result: Int = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: OctagonalNumber = OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
	// k = 10
	task.octagonalNo(10);
}
main();

Output

  1  8  21  40  65  96  133  176  225  280
// Kotlin program for
// Octagonal number
class OctagonalNumber
{
	fun octagonalNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k octagonal number
		while (n <= k)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			val result: Int = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: OctagonalNumber = OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
	// k = 10
	task.octagonalNo(10);
}

Output

 1 8 21 40 65 96 133 176 225 280

Resultant Output Explanation

When we run the provided C code with 'k' = 10, it prints the first 10 octagonal numbers: 1, 8, 21, 40, 65, 96, 133, 176, 225, and 280.

Time Complexity

Let's analyze the time complexity of the given code. The octagonalNo function contains a loop that iterates 'k' times. Inside the loop, each iteration involves simple arithmetic operations to calculate the octagonal number. The time complexity of these operations is constant, O(1).

Therefore, the overall time complexity of the octagonalNo function is O(k), where 'k' is the number of octagonal numbers to be printed. In the provided code, 'k' is set to 10, so the time complexity is O(10), which simplifies to O(1).

The time complexity of the entire program is linear o(k) with respect to the input 'k', as the number of iterations in the loop directly depends on 'k'.





Comment

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