# PHP program to find the sum of the first n natural numbers who are not powers of a specific number ‘k’

To find the sum of the first n natural numbers who are not powers of a specific number ‘k’, the code is as follows −

## Example

Live Demo

<?php
function sum_of_nums($n_val,$k_val)
{
$tot_sum = ($n_val * ($n_val + 1)) / 2;$pow_val = $k_val; while ($pow_val <= $n_val) {$tot_sum -= $pow_val;$pow_val *= $k_val; } return$tot_sum;
}
$n_val = 20;$k_val = 3;
print_r("The sum of fist 20 natural numbers that are not powers of 3 is ");
echo sum_of_nums($n_val,$k_val);
?>

## Output

The sum of fist 20 natural numbers that are not powers of 3 is 198

A function named ‘sum_of_nums’ is defined and it calculates the sum of natural numbers that are not powers of a certain value. The number and the non-power number are passed as parameters to this function. Outside the function, a value each for n and k is defined and the function is called on these values. Relevant output is displayed on the console.