An encoder including a magnetic medium and a magnetic sensor that is opposed to the magnetic medium with intermediary of a gap and moves relative to the magnetic medium, wherein the magnetic medium is magnetized in direction of the relative movement at a pitch λm, the magnetic sensor includes a plurality of magnetoresistive elements (51, 52) whose electrical resistance value changes depending on a magnetic field at a place where the magnetoresistive element is disposed, and a position where the magnetoresistive element is disposed is defined as a reference position and, in addition to the magnetoresistive element of the reference position, the magnetoresistive elements as harmonic reducing patterns are disposed at following positions, with P(n) defined as an n-th prime, wherein n is a natural number, a position offset by λm/(2·P(n)) from the reference position toward at least one side of the direction of the relative movement in plan view, wherein N ≥ n > 1 and N is a natural number satisfying N > 3, and a position further offset by λm/(2·P(L + 1)) from a position offset by λm/(2·P) from the reference position toward at least the one side of the direction of the relative movement in plan view, wherein P is an L-th prime and L is a natural number satisfying 1 < L < N, a position offset by λm/(2·P) = λm/6 and a position offset by λm/(2·P) = λm/10, and a position offset by λm/(2·P)=λm/14 from the reference position toward the one side in plan view, and a position further offset by λm/(2·P(2+1))=λm/10 from the position offset by λm/(2·P)=λm/6 from the reference position toward at least the one side of the direction of the relative movement in plan view, and a position further offset by λm/(2·P(3+1))=λm/14 from the position offset by λm/(2·P)=λm/6 toward said one side of the direction, and a position further offset by λm/(2·P(3+1))=λm/14 from the position offset by λm/(2·P)=λm/10 from the reference position toward said one side of the direction of the relative movement in plan view. a position offset by λm/(2·P(n)) from the reference position toward at least one side of the direction of the relative movement in plan view, wherein N ≥ n > 1 and N is a natural number satisfying N > 3, and a position further offset by λm/(2·P(L + 1)) from a position offset by λm/(2·P) from the reference position toward at least the one side of the direction of the relative movement in plan view, wherein P is an L-th prime and L is a natural number satisfying 1 < L < N, a position offset by λm/(2·P) = λm/6 and a position offset by λm/(2·P) = λm/10, and a position offset by λm/(2·P)=λm/14 from the reference position toward the one side in plan view, and a position further offset by λm/(2·P(2+1))=λm/10 from the position offset by λm/(2·P)=λm/6 from the reference position toward at least the one side of the direction of the relative movement in plan view, and a position further offset by λm/(2·P(3+1))=λm/14 from the position offset by λm/(2·P)=λm/6 toward said one side of the direction, and a position further offset by λm/(2·P(3+1))=λm/14 from the position offset by λm/(2·P)=λm/10 from the reference position toward said one side of the direction of the relative movement in plan view.