Let $n \geq 3$ be an integer. Let $f$ be a function from the set of all integers to itself with the following property: If the integers $a_1,a_2,\ldots,a_n$ form an arithmetic progression, then the numbers
$$f(a_1),f(a_2),\ldots,f(a_n)$$
form an arithmetic progression (possibly constant) in some order. Find all values for $n$ such that the only functions $f$ with this property are the functions of the form $f(x)=cx+d$, where $c$ and $d$ are integers.
1. **Claim:** All prime \( n \) will not work.

2. **Proof:**
   - Consider \( n = 3 \) with \( a_1 = 1, a_2 = 2, a_3 = 3 \). If we pick \( f(a_1), f(a_2), f(a_3) \) in that order, we have:
     \[
     f(1) = a, \quad f(2) = a + b, \quad f(3) = a + 2b
     \]
   - If we keep adding 1 to \( a_1, a_2, a_3 \), we get:
     \[
     f(4) = a + 3b, \quad f(5) = a + 4b, \quad \ldots
     \]
   - Similarly, if we keep subtracting 1 from \( a_1, a_2, a_3 \), we get:
     \[
     f(0) = a - b, \quad f(-1) = a - 2b, \quad \ldots
     \]
   - Thus, we generate \( f(x) = cx + d \).

3. **Counterexample:**
   - Consider \( f(a_1), f(a_3), f(a_2) \) instead. We have:
     \[
     f(1) = a, \quad f(2) = a + 2b, \quad f(3) = a + b
     \]
   - For \( f(4) \), pick \( a_1 = 2, a_2 = 3, a_3 = 4 \):
     \[
     f(2) = a + 2b, \quad f(3) = a + b \implies f(4) = a \text{ or } a + 3b
     \]
   - For \( f(0) \), pick \( a_1 = 0, a_2 = 1, a_3 = 2 \):
     \[
     f(0) = a - 2b \text{ or } a + b
     \]
   - Pick \( a_1 = 0, a_2 = 2, a_3 = 4 \):
     \[
     f(0) = a - 2b \implies f(4) = a - 6b \text{ or } a + 6b
     \]
   - This forces \( f(0) = a + b \).

4. **Pattern Analysis:**
   - We see \( f(e \pm 3k) = f(e) \) for some integer \( k \) and \( e \in [1, n] \).
   - For \( 3 \nmid d \), \( dx \) will cover all residues of 3 for integer \( x \in [0, 2] \).
   - For \( 3 \mid d \), \( f(a_1) = f(a_2) = f(a_3) \) is constant.

5. **Generalization to all prime \( n \):**
   - For any arithmetic sequence \( a_1, a_2, \ldots, a_n \) with common difference \( d \):
     \[
     f(a_1), f(a_2), \ldots, f(a_n) \text{ corresponds to } a, a + b, a + 2b, \ldots, a + (n-1)b \text{ in some order}
     \]
   - For \( n \mid d \), \( f(a_1) = f(a_2) = \ldots = f(a_n) \) is constant.

6. **Composite \( n \):**
   - Consider \( a_1 = 1, a_2 = 2, \ldots, a_n = n \) and \( f(1), f(2), \ldots, f(n) \in \{a, a + b, a + 2b, \ldots, a + (n-1)b\} \).
   - If \( f(1) = f(2) = \ldots = f(n) \), it is a constant sequence.
   - Assume \( f(1), f(2), \ldots, f(n) \) are distinct.
   - For \( a_1 = 2, a_2 = 3, \ldots, a_n = n + 1 \):
     \[
     f(n+1) = f(1) \text{ or } f(n+1) = a + nb
     \]
   - For \( f(n+1) = f(1) \), there exists a divisor \( p \mid n \) with \( 1 < p < n \):
     \[
     a_1 = 1, a_2 = 1 + p, \ldots, a_k = n + 1 \implies f(a_i) = f(n+1) = f(1)
     \]
   - For \( f(n+1) = a + nb \), this forces \( f(x) = a + (x-1)b \) for \( x \ge n+1 \).

7. **Conclusion:**
   - For composite \( n \), the only functions \( f \) with the given property are of the form \( f(x) = cx + d \).

The final answer is \( \boxed{ n } \) must be composite.