
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
To find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’, the code is as follows −
Example
<?php function sum_of_nums($n_val, $x_val, $y_val) { $val_1; $val_2; $val_3; $val_1 = floor(((int)$n_val / $x_val)) * (2 * $x_val + (int)((int)$n_val / $x_val - 1) * $X) / 2; $val_2 = floor(((int)$n_val / $y_val)) * (2 * $y_val + (int)((int)$n_val / $y_val - 1) * $y_val) / 2; $val_3 = floor(((int)$n_val / ($x_val * $y_val))) * (2 * ($x_val * $y_val) + ((int)$n_val / ($x_val * $y_val) - 1) * (int)($x_val * $y_val))/ 2; return ceil($val_1 + ($val_2 - $val_3)); } $n_val = 11; $x_val = 2; $y_val = 5; print_r("The sum of first 11 natural numbers divisible by 2 or 5 is "); echo sum_of_nums($n_val, $x_val, $y_val); ?>
Output
The sum of first 11 natural numbers divisible by 2 or 5 is 15
A function named ‘sum_of_nums’ is defined that computes three values by checking if they can be divided by two specific values or not. Outside the function, the number and the two specific values are defined, and the function is called by passing these values as parameters. Relevant output is displayed on the console
- Related Articles
- Sum of first N natural numbers which are divisible by X or Y
- PHP program to find the sum of the first n natural numbers who are not powers of a specific number ‘k’
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- PHP program to find the first natural number whose factorial can be divided by a number ‘x’
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- PHP program to find the average of the first n natural numbers that are even
- PHP program to find the sum of cubes of the first n natural numbers
- PHP program to find the sum of the 5th powers of first n natural numbers
- PHP program to calculate the sum of square of first n natural numbers
- Smallest number that is divisible by first n numbers in JavaScript
- 8085 program to find the sum of first n natural numbers
- Find the first natural number whose factorial is divisible by x in C++
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- PHP program to find the sum of cubes of natural numbers that are odd
- Find if given number is sum of first n natural numbers in C++

Advertisements