All numbers $Z$, $n$ and the $\mathrm{Threshold}$ are natural numbers.
My inputs are $Z$ and $n$, and I would like to find a minimal $x$ such that
$$\operatorname{LCM}(Z - x, 2^n) \le \mathrm{Threshold}$$
where threshold is an arbitrary constant I can choose and should be in the ballpark of 5000-12000 or therebouts.
$x$ is a natural number, too, including $0$.
For example: $Z=2011, n=5$. Then $x=11$ or $x=3$ as:
> data.frame(candidate = 2000:2011) %>% mutate(lcm = sapply(candidate, function (x) LCM(x, 2**5))) %>% arrange(lcm)
candidate lcm
1 2000 4000
2 2008 8032
3 2004 16032
4 2002 32032
5 2006 32096
6 2010 32160
7 2001 64032
8 2003 64096
9 2005 64160
10 2007 64224
11 2009 64288
12 2011 64352
depending on the choice of $\mathrm{Threshold}$.
My problem is that I don't really know where to start to tackle this. I also don't know if this is solvable directly, or if I need to do it by means of a program.
If I need to do it by a program, I would need to optimize for the number of candidates to calculate the LCM for.
Any pointers greatly appreciated. :)
Notice $$lcm(a, b)=ab/gcd(a, b)$$ you want to $gcd(a, b)$ to be as big as possible when $ab$ is in a certain range.
Notice that $gcd(a, 2^n)=2^m$ for some $m$, so you want to make $Z-x$ contain large divisor of $2^m$.
A quick way might be writing the number in binary representation.
For example, $$2011=(11111011011)_2$$ so when $x\le 64=2^6$, we need to choose the last $6$ binary digits , $(011011)_2=35$. On the other hand, if $Threshold$ is given, you just need to search a few.