Skip to content

Instantly share code, notes, and snippets.

@taless474
Created November 1, 2018 19:29
Show Gist options
  • Save taless474/5b9eb2fc1903cd882b35957b5771f10a to your computer and use it in GitHub Desktop.
Save taless474/5b9eb2fc1903cd882b35957b5771f10a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <limits>
template <typename T, typename F, typename P>
T collision_point(T const& x, F f, P p) {
if (!p(x))
return x;
T slow = x;
T fast = f(x);
while (fast != slow) {
slow = f(slow);
if (!p(fast))
return fast;
fast = f(fast);
if (!p(fast))
return fast;
fast = f(fast);
}
return fast;
}
template <typename F> bool int_predicate(int n, F f) {
int result_int = f(n);
long long result_long = f(n);
if (result_int == result_long)
return true;
return false;
}
long long add_1(int x) { return x + 1ll; }
bool pred(int n) { return int_predicate(n, add_1); }
int main() {
std::cout << collision_point(std::numeric_limits<int>::max() - 1, add_1, pred) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment