The problem you have is that, when you put the primes into the array, that you, for each of the array indexes keep walking through every value and keep overwriting the previously found one
=> ending up with an array where each index contains the maximum prime below the value entered.