K
K
Kerson2021-03-10 22:48:13
Pascal
Kerson, 2021-03-10 22:48:13

A simple task in Pascal?

The set of integers belonging to the numerical segment [1000;
9999], which satisfy the following conditions:
− are not divisible by 5, 7 and 11;
− record in ternary number system has exactly 8 digits.
Find the minimum and maximum of these numbers.

I did this, but something doesn't work:

program Z33;
var max,x1,min,p,x,k:integer;
beginmax
:=1000;
k:=0;
min:=9999;
For x:= 1000 to 9999 do
begin
x1:=x;
while x1 div 3>=2 do
begin
p:=x1 mod 3;
k:=k+1;
x1:=x1 div 3;
end;
k:=k+1;
if (k=8)then
begin
if ((x mod 5<>0) and (x mod 7 <>0)and (x mod 11 <>0)) then
begin
if min>x then
begin
min:=x;
end;
if max begin
max:=x;
end;
end;
end;
end;
WriteIn(min,' ',max);
end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HemulGM, 2021-03-11
@HemulGM

Before the logical start of using k - you don't reset it.
Optimization: It is logical to reset it to 1 than to increase it by 1 after checking.
The variable p is not used in any way either by itself or by its value that you calculate for it in the loop.

program Z33;

var
  max, x1, min, x, k: integer;

begin
  max := 1000;
  min := 9999;
  for x := 1000 to 9999 do
  begin
    x1 := x;
    k := 1;
    while x1 div 3 >= 2 do
    begin
      Inc(k);
      x1 := x1 div 3;
    end;
    if k = 8 then
    begin
      if (x mod 5 <> 0) and (x mod 7 <> 0) and (x mod 11 <> 0) then
      begin
        if min > x then
          min := x;
        if max < x then
          max := x;
      end;
    end;
  end;
  Writeln(min, ' ', max);
end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question