Let $R$ be a commutative Noetherian ring with identity. $M$ is a finitely generated projective $R$-module. Then for any $r$, the set $\{P\ \text{is a prime of} \ R|\operatorname{rank} M_P=r\}$ is an open set of $\operatorname{Spec}R$.
For a finitely generated $R$-module $M$, I know the set $\{P\ \text{is a prime of } R|M_P\ \text{can be generated by} \leq r\ \text{elements over $R_P$}\}$ is an open set of $\operatorname{Spec}R$. In this case, I somehow need to show a set similar to the one mentioned above but replacing $\leq$ with $=$ is open in $\operatorname{Spec}R$. But I can only show the complement of such a set is the union of a closed set and an open set. I have no idea how to proceed. Any hints or suggestions are appreciated. Thanks.
$M$ is projective of finite presentation, thus for any prime $p$, $M_p$ is free of a certain rank. As a consequence, $M$ is locally free : pick a basis of $M_p$, by sections defined on some open neighbourhood $U$ of $p$. This gives a morphism $O_U^r\rightarrow M_{|U}$, This morphism is surjective in a (possibly smaller) neighbourhood of $p$.
To show that that this morphism is also injective, use the fact that $M$ is noetherian, so any finite set of generators gives a finite presentation of $M$ : $O_U^n\rightarrow O_U^r\rightarrow M_{|U} \rightarrow 0$. We see that the morphism $O_U^n\rightarrow O_U^r$ zero in some neighbourhood of $p$.
So $M$ is locally free, and the rank is a locally constant function on Spec(R).