- Trending Categories
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
Find a positive number M such that gcd(N^M,N&M) is maximum in Python
Suppose we have a number N, we have to find a positive number M such that gcd(N^M, N&M) is as large as possible and m < n. We will also return the largest gcd thus obtained.
So, if the input is like 20, then the output will be 31
To solve this, we will follow these steps −
- if bit_count(n) is same as 0, then
- for i in range 2 to int(square root of (n)) + 1, do
- if n mod i is same as 0, then
- return int(n / i)
- if n mod i is same as 0, then
- for i in range 2 to int(square root of (n)) + 1, do
- otherwise,
- val := 0
- p :=
- dupn := n
- while n is non-zero, do
- if (n AND 1) is same as 0, then
- val := val + p
- p := p * 2
- n := n >> 1
- if (n AND 1) is same as 0, then
- return gcd(val XOR dupn, val AND dupn)
- return 1
Example
Let us see the following implementation to get better understanding −
from math import gcd, sqrt def bit_count(n): if (n == 0): return 0 else: return (((n & 1) == 0) + bit_count(n >> 1)) def maximum_gcd(n): if (bit_count(n) == 0): for i in range(2, int(sqrt(n)) + 1): if (n % i == 0): return int(n / i) else: val = 0 p = 1 dupn = n while (n): if ((n & 1) == 0): val += p p = p * 2 n = n >> 1 return gcd(val ^ dupn, val & dupn) return 1 n = 20 print(maximum_gcd(n))
Input
20
Output
31
- Related Articles
- Program to find number m such that it has n number of 0s at end in Python
- If $l, m, n$ are three lines such that $l \parallel m$ and $n \perp l$, prove that $n \perp m$.
- It is given that\[63.63=m\left(21+\frac{n}{100}\right)\]Where \( m, n \) are positive integers and \( n
- A godown measures $40\ m \times 25\ m \times 10\ m$. Find the maximum number of wooden crates each measuring $1.5\ m \times 1.25\ m \times 0.5\ m$ that can be stored in the godown.
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
- If \( a=x^{m+n} y^{l}, b=x^{n+l} y^{m} \) and \( c=x^{l+m} y^{n} \), prove that \( a^{m-n} b^{n-1} c^{l-m}=1 . \)
- If \( x=a^{m+n}, y=a^{n+1} \) and \( z=a^{l+m} \), prove that \( x^{m} y^{n} z^{l}=x^{n} y^{l} z^{m} \)
- A godown measures \( 40 \mathrm{~m} \times 25 \mathrm{~m} \times 15 \mathrm{~m} \). Find the maximum number of wooden crates each measuring \( 1.5 \mathrm{~m} \times 1.25 \mathrm{~m} \times 0.5 \mathrm{~m} \) that can be stored in the godown.
- Program to compare m^n and n^m
- Construct DPDA for a(n+m)bmcn n,m≥1 in TOC
- The roots of the equation $x^{2} -3x-m( m+3) =0$, where m is a constant, are:$( A) m, m+3$$( B)-m, m+3$$( C)m, -(m+3)$$( D)-m,-(m+3)$
- Find the value of : $\frac{1}{1+a^{n-m}}+\frac{1}{1+a^{m-n}}$.
- If $M$ is the mean of $x_1, x_2, x_3, x_4, x_5$ and $x_6$, prove that$(x_1 - M) + (x_2 - M) + (x_3 - M) + (x_4 - M) + (x_5 - M) + (x_6 - M) = 0$.
- Pick maximum sum M elements such that contiguous repetitions do not exceed K in C++
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N in C++

Advertisements