For some N (like 2,5,10) m is just 1 (second element of C is already greater than the root of N) and so your calculation of the rearranged vector c fails. Either include another if-statement to cope with that case (then c<--C) or do it completely different like in the attached.
![1.png]()
![0.png]()