You are on page 1of 1

Tutorials Student Jobs Courses Sign In

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python Competitive Programming Machine Learning Web Dev

Table of Contents

Program for Fibonacci


numbers Power in Mathematics
Write a program to print all Last Updated : 03 May, 2021
permutations of a given string

C++ Data Types The power of a number says how many times to use the number in a
multiplication. Powers are also called Exponents or Indices. For
Set in C++ Standard Template example, 8^2 could be called “8 to the power 2” or “8 to the second
Library (STL)
power”, or simply “8 squared”.
Coin Change | DP-7

Sieve of Eratosthenes

Program to find GCD or HCF


of two numbers

Program for Decimal to Binary


Conversion

Prime Numbers

Merge two sorted arrays

Modulo Operator (%) in C/C++


with Examples W H AT ' S N E W
Some interesting fact about Power :

Complete
1. If the indices is 1, then you just have the number itself. For example, Interview
5^1 = 5
Preparation
2. If the indices is 0, then you get 1. For example, 5^0 = 1 View Details
3. Exponents make it easier to write and use many multiplications
4. Negative exponent means how many times to divide one by the
number.For example, 5^-1 = 1 /5 = 0.2 System Design-
Live Classes for
How we check if a number is power of y for a given integer x ? Working
Naive solution: Professionals
Given two positive numbers x and y, check if y is a power of x or not. View Details

Examples :

Data Structures &


Algorithms- Self
Input: x = 10, y = 1 Paced Course
Output: True View Details

Input: x = 10, y = 1000


Output: True

Input: x = 10, y = 1001


Output: False

Approach : A simple solution is to repeatedly compute powers of x. If a


power becomes equal to y, then y is a power, else not.

C++ Java Python3 C# PHP Javascript

// C++ program to check if a number is power of


// another number
#include <bits/stdc++.h>
using namespace std;

/* Returns 1 if y is a power of x */
bool isPower(int x, long int y)
{
// The only power of 1 is 1 itself
if (x == 1)
return (y == 1);

// Repeatedly comput power of x


long int pow = 1;
while (pow < y)
pow *= x;
M O ST P O P U L A R I N
// Check if power of x becomes y M AT H E M AT I C A L
return (pow == y);
}
Calculation of address of element of
/* Driver program to test above function */
1-D, 2-D, and 3-D using row-major
int main()
{ and column-major order
cout << (isPower(10, 1) ? "True" : "False") << endl;
cout << (isPower(1, 20) ? "True" : "False") << endl;
cout << (isPower(2, 128) ? "True" : "False") << endl; Segment Tree | Set 1 (Sum of given
cout << (isPower(2, 30) ? "True" : "False") << endl; range)
return 0;
}
Program to multiply two matrices

Program to print prime numbers


Output:
from 1 to N.

Modular multiplicative inverse


True
False
True
False

Time complexity of above solution is O(Logxy)


Basic Program related to Power :

Find unit digit of x raised to power y


Check if a number can be expressed as x^y (x raised to power y)
Find the multiple of x which is closest to a^b
All possible numbers of N digits and base B without leading zeros
Check if a number can be expressed as a^b | Set 2
Write you own Power without using multiplication(*) and
division(/) operators

More problems related to Powers :

Total number of subsets in which the product of the elements is


even
Sum of first N natural numbers which are not powers of K
GCD of a number raised to some power and another number
Largest N digit number divisible by given three numbers
Check whether a given Number is Power-Isolated or not

M O ST V I S I T E D I N
Recent Articles on Power!
SCHOOL
PROGRAMMING
Attention reader! Don’t stop learning now. Get hold of all the
important mathematical concepts for competitive programming with Python Dictionary
the Essential Maths for CP Course at a student-friendly price. To
complete your preparation from learning a language to DS Algo and Arrays in C/C++
many more, please refer Complete Interview Preparation Course.
Reverse a string in Java

Types of Operating Systems

Like 0 C++ Classes and Objects

Previous Next

Harmonic Progression Capgemini Interview


Experience 2018- Senior
Analyst

R ECO M M E N D E D A RT I C L E S Page : 1 2 3

01 Check if given number


is a power of d where d 05 Maximize the product
of sum and least power
is a power of 2 by choosing at most K
05, Jun 18 elements from given
value and power Arrays
28, Oct 21

02 Compute power of
power k times % m
28, Jun 18 06 Find power of power
under mod of a prime
22, Feb 17

03 Larger of a^b or b^a (a


raised to power b or b 07 Mathematics | L U
Decomposition of a
raised to power a) System of Linear
08, Dec 18 Equations
26, Jun 15

04 Find the sum of power


of bit count raised to 08 Mathematics | Mean,
Variance and Standard
the power B Deviation
09, Jan 20 10, Jul 15

Article Contributed By :

ABHISHEK TIWARI 13
@ABHISHEK TIWARI 13

Vote for difficulty

Easy Normal Medium Hard Expert

Improved By : jana_sayantan

Article Tags : maths-power, Mathematical, School Programming

Practice Tags : Mathematical

Improve Article Report Issue

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

Company Learn News Languages Web Contribute


5th Floor, A-118, About Us Algorithms Technology Python Development Write an Article
Sector-136, Noida, Uttar
Pradesh - 201305 Careers Data Work & Java Web Tutorials Pick Topics to
feedback@geeksforgeeks.org Structures Career HTML Write
Privacy CPP
Policy Machine Business Golang CSS Write Interview

Contact Us learning Finance Experience


C# JavaScript
Copyright CS Lifestyle Internships
Bootstrap
Policy Subjects Video Internship
Video
Tutorials

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie
Got It !
Policy & Privacy Policy
@geeksforgeeks , Some rights reserved

You might also like