E
E
EvgeniyRava2015-12-09 09:13:12
Objective-C
EvgeniyRava, 2015-12-09 09:13:12

What needs to be done in the assignment?

There is a program for creating a table of prime 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;
}

According to the assignment it is necessary
Program 6.10 in some cases works inefficiently. For example, this applies to checking even numbers. Since any even number greater than 2 cannot be prime, the program could skip all even numbers as both possible primes and possible divisors. The inner for loop is also inefficient because p is always dividing by all the values ​​of d from 2 to p-1. To avoid this inefficiency, you can add cases to the for clauses. Then you can have the for loop run until the divisor is found and d is less than p. Modify Program 6.10 to make these two changes, then run the program to see if it works.
It turns out like this
for (d = 2; d<p || cases == 1; ++d)
? How to add it to the loop if before the loop we declare the value of the variable cases 1; when the loop starts, the cases variable is 1 by default, and I need to exit the loop when the value is 1. It turns out that I need to initially declare the value of cases 0 and check in the condition when it will be equal to 1?
And about that
Since any even number greater than 2 cannot be prime, the program could skip all even numbers both as possible prime numbers and as possible divisors.
It looks (p % 2 == 0 || p % 5 == 0)like I did it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-12-09
@alexey-m-ukolov

Why add Cases to the condition? After all, Cases is roughly zero value 1-True, 0-False.
It is then to add - to interrupt the execution of the loop when the value has already been found.
Loop until Cases is 1?
It says add to for, not replace , so that's not the only condition.
The program turns out to be in cycles or I am mistaken?
You are wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question