- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
We are given a number N. The goal is to find the pairs of numbers from 1 to N such that the product of pairs is equal to the sum of pairs.
Let us understand with examples.
Input − N=11
Output − Count of pairs of no. from 1 to N with Product divisible by their Sum are − 1
Explanation − Numbers 3 and 6 have product 18 and their sum 9 fully divides 18.
Input − N=30
Output − Count of pairs of no. from 1 to N with Product divisible by their Sum are − 12
Explanation − Pairs are − (3, 6), (4,12), (5, 20), (6, 12), (6, 30), (8, 24), (9, 18), (10, 15), (12, 24), (15, 30), (20, 30), (21, 28)
Count of pairs − 12
The approach used in the below program is as follows
We will traverse from 1 to N using FOR loop twice. For every, i search for j such that the product of (i,j) is divisible by sum i+j. Increment count for suc i,j pairs such that i!=j.
Take a number N as input.
Function Sum_N(N) takes N and returns the count of pairs such that the product of numbers is divisible by sum of numbers.
Traverse from i=1 to i<N.
Travers from j=i+1 to j<=N.
Take the initial count as 0.
For each i and j calculate temp= (i*j)%(i+j).
If temp is 0 then sum fully divides the product. Increment count.
After the end of all iterations, the count will have a total number of such pairs.
Return count as result.
Example
#include <bits/stdc++.h> using namespace std; int Sum_N(int N){ int count = 0; for (int i = 1; i < N; i++){ for (int j = i + 1; j <= N; j++){ int temp = (j * i) % (j + i); if (!temp){ count++; } } } return count; } int main(){ int N = 20; cout<<"Count of pairs of numbers from 1 to N with Product divisible by their Sum are: "<<Sum_N(N); return 0; }
Output
If we run the above code it will generate the following output −
Count of pairs of numbers from 1 to N with Product divisible by their Sum are: 6
- Related Articles
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N in C++
- Check if product of first N natural numbers is divisible by their sum in Python
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Count ordered pairs with product less than N in C++
- Count pairs in array whose sum is divisible by 4 in C++
- Count pairs in array whose sum is divisible by K in C++
- Count n digit numbers divisible by given number in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Smallest possible number divisible by all numbers from 1 to n in JavaScript
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Find count of Almost Prime numbers from 1 to N in C++
- Count Pairs from two arrays with even sum in C++
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Queries to count the number of unordered co-prime pairs from 1 to N in C++
