G
G
g1shy \(#_#)/2020-10-27 18:11:09
Python
g1shy \(#_#)/, 2020-10-27 18:11:09

How is this task from the Unified State Examination in Informatics solved?

The essence of the problem: you need to write a program that searches among integers belonging to the numerical interval [174457; 174505], numbers that have exactly two different natural divisors, not counting one and the number itself. For each found number, write these two divisors in two adjacent columns on the screen from a new line in ascending order of the product of these two divisors. Divisors in a string must also be in ascending order.

For example, in the range [5; 9] exactly two different natural divisors have numbers 6 and 8, so for this range the output on the screen should contain the following values:

2 3

2 4

The way I tried to solve this problem:

a = 174457
b = 174505
k = 0
d = []
for n in range(a, b+1):
    if n%2 == 0:
        k+=1
        d.append(n)
        if k > 2:
            break
    if k == 2:
        print(d[0], d[1])

I just don't have the brains to figure out how to find these natural divisors

. Pascal code was provided in the answers to this problem:
var 
    x, numDel, i, j: longint;
    d: array[1..2] of longint;
begin
    for i := 174457 to 174505 do begin
        numDel := 0;
        for j := 2 to i div 2 do begin
            if i mod j = 0 then begin
                numDel := numDel + 1;
                if numDel > 2 then break;
                d[numDel] := j;
            end;
        end;
        if numDel = 2 then writeln(d[1], ' ', d[2]);
    end;
end.

But, when I tried to do something similar in Python, the result came out even more wrong.
Here is what my program outputs:

174458 174460
174458 174460

And this should be:

3 58153
7 24923
59 2957
13 13421
149 1171
5 34897
211 827
2 87251

Please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2020-10-27
@g1shy

So what?

a = 174457
b = 174505
k = 0
for n in range(a, b + 1):
    ds = []
    for d in range(2, n//2 + 1):
        if n % d == 0:
            ds.append(d)
            if len(ds) > 2:
                break
    if len(ds) == 2:
        print(ds[0], ds[1])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question