Home python Search for various native natural dividers in a certain interval

# Search for various native natural dividers in a certain interval

Author

Date

Category

You need to write a program that searches among integers belonging to a numerical segment [125 256; 125 330], numbers having exactly six different nominal divisors. For each number found, write these six divisors in six adjacent columns on the screen from a new line. Dividers in the row must follow in ascending order.

for example, in the range [2; 48] Exactly six point different natural divisors have numbers 24, 36 and 40, so for this range the output on the screen must contain the following values:

2 4 6 8 12 24

2 4 6 12 18 36

2 4 8 10 20 40

How I tried to solve this task:

``````a = 125256
B = 125330.
For n in Range (A, B + 1):
DS = []
For D in Range (2, N // 2 + 1): Honestly, I do not fully understand what function does N // 2 + 1 perform here.
IF N% d == 0:
DS.APPEND (D)
IF LEN (DS) & GT; 6:
Break
IF LEN (DS) == 6:
Print (DS [0], DS [1], DS [2], DS [3], DS [4], DS [5])
In my opinion, the 7th line should look like this: if n% d == 0 and d% 2 == 0:
# But then nothing is displayed at all
``````

I would like to hear from people who are trying in Pajton, what I do wrong.

Well, this option is correctly running your test example:

``````a = 2 # 125256
B = 48 # 125330
k = 0.
For n in Range (A, B + 1, 2):
DS = []
For D in Range (2, N // 2 + 1, 2):
IF N% d == 0:
DS.APPEND (D)
IF LEN (DS) & GT; 6:
Break
DS.APPEND (N)
IF LEN (DS) == 6:
Print (n, '-', ds [0], ds [1], ds [2], ds [3], ds [4], ds [5])
``````

Conclusion:

``````24 - 2 4 6 8 12 24
36 - 2 4 6 12 18 36
40 - 2 4 8 10 20 40
``````

Explanations:

• Catching makes sense only for even numbers, in odd numbers there will not be aware divisors, so the step `2 `in the cycle by `N `(reducing the bust 2 times)
• Dividers also makes sense to sore only – because such a condition, therefore, in the cycle by `D `, I also put a step `2 `(reducing the bust 2 times)
• The number itself is also considered to be its divider, judging by the above example, so I added it to the list manually after the cycle end. You can do this before the start of the cycle, but then the list of numbers do not turn out in order, so I add at the end, just for beauty.
• Bruep made to `n // 2 + 1 `Because if the number is exactly, it means it is divided into `2 `, dividers less than `2 `It cannot be, it means that the bust makes sense to lead only to `n // 2 `(and I add `N `later), `+1 `Due to the features of the `Range `– the final value is not included in the bust, so you need to add `1 `(total – again reduce the bust by 2 times).

Result for numbers from the title:

``````125262 - 2 6 18 13918 41754 125262
125288 - 2 4 8 31322 62644 125288
125298 - 2 6 18 13922 41766 125298
``````

``````for d in range (2, n // 2 + 1): # Honestly, I do not fully understand, What function performs here n // 2 + 1
``````

i.e. You wrote the code and do not understand what you wrote? Or did you write it off?

Well, you first need to figure out how the algorithm

should look like

If we assume that 1 is not a divider, then you need

1. For each number `N `Go from the number `I `from 2 to `n `inclusive and try to divide the number `N `on `i `

2. If the division occurs without a residue (the module is 0) and the divider is even, then +1 to the number of found divisors

``````for n in range (a, b + 1):
Count = 0.
For j in Range (2, n + 1):
IF N% j == 0 and j% 2 == 0:
Count + = 1
IF Count == 6:
Print ("found")
``````

Obviously, `N `is a divider for himself, and the nearest smaller divider is `n / 2 `, just because `0 & lt; n / (n / 2 + j) & lt; 1 `

Therefore, the divider counter can be started with 1 and sort out the numbers only to `N / 2 `:

The only thing that first need to check whether `N `is even

``````count = int (n% 2 == 0)
``````

not to do a lot of `if else `just translated boolean value to integer

``````for n in range (a, b + 1):
Count = int (N% 2 == 0)
For j in Range (2, N // 2 + 1):
IF N% j == 0 and j% 2 == 0:
Count + = 1
IF Count == 6:
Print ("found")
``````

In this case,

``````n // 2 + 1
``````

To guarantee half of the number `N `when proeding

If you need to withdraw dividers, then only in the code it is necessary to add their collection (take into account that 1 divider we always know – this is `N `)

``````for n in range (a, b + 1):
Count = int (n% 2 == 0)
Dividers = [N] IF N% 2 == 0 ELSE []
For j in Range (2, N // 2 + 1):
IF N% j == 0 and j% 2 == 0:
Count + = 1
Dividers.APPEND (J)
IF Count == 6:
Print (* Sorted (Dividers))
``````

If you edit your code, the following is:

``````a = 125256
B = 125330.
For n in Range (A, B + 1):
DS = []
For D in Range (2, n + 1): # because a and b small, you can consider all dividers of the number
IF N% d == 0:
if d% 2 == 0: ds.append (D) # Right, in this step we check that the divider is even
# If Len (DS) & GT; 6: Excessive code, slightly accelerates the decision
# Break
IF LEN (DS) == 6:
Print (DS [0], DS [1], DS [2], DS [3], DS [4], DS [5])
``````

The optimal solution is:

1. go through all numbers
2. each number divide on all numbers to the root inclusive.
3. Every time we get two divisors (divider and private), if some of them are even – we save.
``````for n in range (125256, 125330 +1):
Counter = Set () # Most instead of the list, avoid repeats
For D in Range (1, int (n ** 0.5) + 1): # Bruel to root inclusive
if n% d == d% 2 == 0: counter.add (D) # save the divider if it is even
if n% d == n // D% 2 == 0: counter.add (N // D) # save the private (it is also divided into it), if it is even
If Len (Counter) == 6: # If divisters six
Print (* Sorted (Counter) # withdraw them in ascending order
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.