- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Program to check whether a number is Proth number or not in C++
Given with a number ‘n’ and the task is to determine whether the given positive integer is a proth or not and display the result as an output.
What is Proth Number?
A proth number is given by
$$N=k\cdot\:2^{n}+1$$
Where, n is a positive integer and k is a odd positive integer
The first few proth numbers are given below −
3, 5, 9, 13, 17, 25, 33, 41, 49, 57, 65, 81, 97.......
Input
number: 17
Output
its a proth number
Input
number: 18
Output
its not a proth number
Approach used in the given program is as follows
Input the number to check for the condition
Apply the given formula to check whether its a proth number or not
If the condition holds true print its a proth number
If the condition doesn’t holds true print its not a proth number
Algorithm
Step 1→ declare function to calculate power of 2 bool isPower(int num) return (num && !(num & (num - 1))) Step 2→ Declare function to check if a number is a proth number or not bool isProth(int num) declare int k = 1 While (k < (num / k)) IF (num % k == 0) IF (isPower(num / k)) return true End Set k = k + 2 End End return false Step 3→ In main() Declare int num = 17 IF (isProth(num - 1)) Print "its a proth number" End Else Print "its not a proth number" End
Example
#include <bits/stdc++.h> using namespace std; //function to calculate power of 2 bool isPower(int num){ return (num && !(num & (num - 1))); } //function to check if a number is a proth number bool isProth(int num){ int k = 1; while (k < (num / k)){ if (num % k == 0){ if (isPower(num / k)) return true; } k = k + 2; } return false; } int main(){ int num = 17; if (isProth(num - 1)) cout << "its a proth number"; else cout << "its not a proth number"; return 0; }
Output
If run the above code it will generate the following output −
its a proth number
Advertisements