TITLE: Give an example of a real function so that every rational is a strict local minimum
QUESTION [12 upvotes]: Give an example of $f : \mathbb R → [0, \infty) $ so that every $r \in \mathbb Q$ is
  a strict local minimum for $f$.

Strict local minimum means there is a vicinity $V$ of $r$ such that $f(y) > f(r) ,\ \forall y \in V-\{r\}$

My attempt
So far, none. My feeling is there isn't such a function, mainly because of the density of $\mathbb Q$ in $\mathbb R$. Suppose I define $f$ like this: $f(x) = 0$ for $x \in \mathbb Q$ and $f(x) = 1$ for $x \not \in \mathbb Q$. Every rational $r$ does not map to a strict local minimum for $f$ only because of the other rationals present in every vicinity of $r$. So $f$ cannot be constant on $\mathbb Q$, but how to define it is beyond my imagination.

REPLY [12 votes]: I think that the function that sends any irrational number on $1$ and sends a rational $\frac{p}q$ (where the fraction is irreducible) on $1-\frac1q$ does the job. Given any rational number $\frac{p}q$, you can always find a neighborhood so that any rational number has a denominator bigger than $q$

REPLY [11 votes]: Here's a pretty direct hint. If there were only rationals, the function could be defined as the denominator in lowest terms, because everything nearby enough would have a larger denominator. If we want to also define it on the irrationals this wouldn't work. However, we can compress the positive integers into $[0,1)$ in an order preserving way, then we can make the function $1$ on the irrationals.