#include<bits/stdc++.h>
using namespace std;

mt19937_64 rd(time(0));
int rand(int L, int R){
    return L + rd() % (R - L + 1);
}

int n, arr[10000];
bool is_prime[10000];

void eratosthenes(){
    memset(is_prime, 1, sizeof is_prime);
    is_prime[0] = is_prime[1] = 0;

    for(int i = 2; i*i < n; i++){
        if(is_prime[i]){
            for(int j = i*i; j < n; j += i) is_prime[j] = 0;
        }
    }
}

void prime_sort(){
    for(int i = 0; i < n; i++){
        if(is_prime[i]){
            int times = i;

            while(times > 0){
                int j = rand(0, n - 1);
                if(arr[min(i, j)] < arr[max(i, j)]) swap(arr[min(i, j)], arr[max(i, j)]);
                times--;
            }
        }
    }
}

bool isSorted(int* arr, int length){
    for(int i = 1; i < length; i++){
        if(arr[i - 1] < arr[i]) return 0;
    }

    return 1;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n;
    for(int i = 0; i < n; i++) cin >> arr[i];

    eratosthenes();

    while(!isSorted(arr, n)){
        prime_sort();
    }

    for(int i = 0; i < n; i++) cout << arr[i] << ' ';

    return 0;
}
