Problem in CHMOD

,

THough I did this question by the prime number approach but
Why does this naive approach gives a WA
https://www.codechef.com/viewsolution/29017766