Calculate the sum of potencies

In this exercise, you will calculate the sum of potencies recursively. This mathematical series is defined as:

  • result=1 for step = 1
  • result + step^step for step > 1

The numbers in this series are getting large very quickly and the series does not converge. The task of this exercise is to calculate the sum of potencies for step = 9.

Este exercício faz parte do curso

Hierarchical and Recursive Queries in SQL Server

Ver Curso

Instruções de exercício

  • Define the CTE calculate_potencies with the fields step and result.
  • Initialize step and result using the definition of the mathematical series as a guide.
  • Add the next step to POWER(step + 1.. and add to result.

Exercício interativo prático

Experimente este exercício preenchendo este código de exemplo.

-- Define the CTE calculate_potencies with the fields step and result
WITH ___ (___, ___) AS (
    SELECT 
  		-- Initialize step and result
  		___,
  		___
    UNION ALL
    SELECT 
  		step + 1,
  		-- Add the POWER calculation to the result 
  		___ + POWER(step + 1, ___ + 1)
    FROM calculate_potencies
    WHERE step < 9)
    
SELECT 
	step, 
    result
FROM calculate_potencies;