The problem with your code is that the
if(sum(check_sum(k,1:1022).*check_sum(k,1:1022))==1022)
is never true. Due to that, your num_right variable is never modified.
If you debug the code, you will find that the first time you hit that if statement you have the following:
sum(check_sum(k,1:1022).*check_sum(k,1:1022)
ans =
103968206
and
size(check_sum(k,1:1022).*check_sum(k,1:1022))
ans =
1 1022
Having said that, I think you might be checking for the wrong thing.
What I would suggest is try debugging your code in order to understand how it works and what you can do to change it.