Question:
Given the following code snippet:
bool foo(int n) {
for(int i=3;i<sqrt(n)+0.5;i+=2)
{
if((n%i)==0){
return false;
}
}
return true;
}
Can you figure out what is the purpose of the function foo ?
Well,On first look it may seems that foo is checking for prime numbers but it is not the case.I wrote a small test program and got this output:
foo returns true for these numbers between 1 to 100:
1 2 3 4 5 6 7 8 10 11 13 14 16 17 19 20 22 23 26 28 29 31 32 34 37 38 41 43 44 4 6 47 52 53 58 59 61 62 64 67 68 71 73 74 76 79 82 83 86 88 89 92 94 97
foo returns false for these numbers between 1 to 100:
9 12 15 18 21 24 25 27 30 33 35 36 39 40 42 45 48 49 50 51 54 55 56 57 60 63 65 66 69 70 72 75 77 78 80 81 84 85 87 90 91 93 95 96 98 99 100
I am unable to understand what the foo is doing from the series.