Dobut regarding prime numbers logic

like i searched in google various website have written condition i<=n/2 where n is number entered
but i cant understand where as i<=n-1; give same result
please explain n/2; logic in prime numbers

Hey @Diwyanshu
It’s okay to check till n-1
We check till n/2 because it’s an optimized way
And checking till n/2 would also be correct as the largest factor of a number can be n/2, because the smallest number which can divide a number for the number to be non-prime is 2, so largest factor can be n/2, so we check till n/2, beyond n/2, factors won’t be found, so checking till n-1 and n/2 are both okay, checking till n/2 is more optimized as it takes less no of steps and less time.

@mahimahans111 thank you mam

1 Like