Help meet the objective C
-
There's a program to create a table of simple numbers.
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
int p,d,cases;
for (p=1; p<=100; ++p) {
cases = 1;
for (d = 2; d<p; ++d)
if (p % d == 0)
cases = 0;
if (cases != 0)
NSLog(@"%i",p);
}
}
return 0;
}
You have to do the job.
Programme 6.10 is in some cases ineffective. For example, this is about checking even numbers. Since any single number of more than 2 cannot be simple, the programme could pass all the even numbers, as possible simple numbers, and as possible entrepreneurs. The internal cycle for also works ineffectively, as all values d between 2 and p-1 are divided for p. In order to avoid this inefficiency, the meaning of cases can be added to the conditions for. Then you can ask for the cycle for as long as there's no dirt and d's less p. Modify programme 6.10 to make these two changes, then implement the programme to verify its work.Sort of.
for (d = 2; d<p || cases == 1; ++d)
? How to add it in the cycle if we declare the value of variable cases 1 before the cycle; when the cycle begins, the default variable cases 1, and I need to leave the cycle when value 1. Is it necessary to declare the value of cases 0 initially and to verify when it is 1?
About that.
Since any single number of more than 2 cannot be simple, the programme could pass all the even numbers, as possible simple numbers, and as possible entrepreneurs.It's coming.
(p % 2 == 0 || p % 5 == 0)
Did I do the right thing?
-
Well, let's just say that (2 just out and not in the algorithm)
int main(int argc, const char * argv[]) { @autoreleasepool { int p,d,cases; NSLog(@"2"); for (p=3; p<=100; p+=2) { cases = 1; for (d = 3; d<sqrt(p); d+=2) if (p % d == 0) { cases = 0; break; } if (cases != 0) NSLog(@"%i",p); } } return 0; }