I have a task:

Find all natural three-digit numbers, each of which has the following properties:

  1. the first digit is three times less than the last digit;
  2. the sum of the number itself, with the number resulting from the permutation of the second and third digits, is divisible by 8 without a remainder.

I have done only with the first property ... with the second I do not know how to do ...

var i,a,p,b,c,o : integer; begin for i:=100 to 999 do if i div 100*3=i mod 10 then writeln(i); end. 
  • what is "-1" ??? why is it so negative ... i just need help - Indentikon
  • You were able to get the digits of the number - combine them with "a number resulting from a permutation of the second and third digits." Well, the cycle makes sense no further than 399. - Igor
  • @Igor how? ........ - Indentikon
  • Number of hundreds * 100 + number of units * 10 + number of tens - Igor
  • 3
    and you yourself wrote the code? Can you explain what is happening in it? - Grundy

2 answers 2

It is not necessary to solve the problem in the forehead. It is worth thinking a little and apply mathematics. We know that the first digit of a number is three times less than the last. So it can not exceed 3. The last digit is the first multiplied by 3. So we need to try the numbers from 1 to 3 for the first position and find the right number for the second position.

Let the first digit of the number i , and the second k , then the second condition of the problem looks like this:

 сотни десятки единицы i*100 + k*10 + i*3 + { само число } i*100 + i*3*10 + k { переставлены 2 и 3 цифры } ----------- 

Simplify the expression:

 (i*100 + k*10 + i*3) + (i*100 + i*3*10 + k) = i*200 + k*10 + k + i*3 + i*30 = i*200 + k*11 + i*33 = i*233 + k*11 

It remains to verify the divisibility of the obtained expression by 8.

 var i,k : integer; begin for i:=1 to 3 do for k:=0 to 9 do if (i*233+k*11) mod 8 = 0 then writeln(i*103+k*10); end. 
  • +1 But in real life, such a “smart” code needs a bunch of comments explaining what it does and why) - Kromster

Let's try to figure out the steps:

  1. It is necessary to go through all three-digit numbers (let's forget op optimizations for the sake of clarity)
  2. For each number you need to get it 1 and 3 digits.
  3. We must compare them and decide whether they are suitable for the condition.
  4. Similarly, the second rule should be processed:
  5. Get the "second" number by changing the 2 and 3 digits
  6. Add to the number and check the remainder of the division by 8

With the first rule, you sorted out the grief in half. Let's see the second in more detail:

How to change the numbers 2 and 3 in numbers - we again divide the number into numbers and collect in a different order:

 i_100 := i div 100; // сотни i_10 := i div 10 mod 10; // десятки i_1 := i mod 10; // единицы i_new := i_100 * 100 + i_1 * 10 + i_10 * 1; 

Check the remainder of the division i + i_new you hope so can yourself.