turbo pascal

D

demon311

Số hàon chỉnh là số gì vậy? Pascal có nhiều thuật ngữ về số lộn tùng phèo hết à....
 
H

huutoan00

Mã:
uses crt;
var i,x:integer;

function TinhTongUoc(x:integer);
var i:integer;
begin
 for i:=2 to trunc(sqrt(x)) do 
  begin
   if x mod i =0 then TinhTongUoc:=TinhTongUoc+i;
  end;
end;

begin
 clrscr;
 Write('Nhap so n: ');readln(x);
 write('Cac so hoan chinh tu 1 den ',x,' la: ');
 for i:=1 to x do 
  begin
   if TinhTongUoc(i)=i then write(' ',i,',');
  end;
readln
end.
 
L

lamnguyen.rs

Mã:
uses crt;
var i,x:integer;

function TinhTongUoc(x:integer);
var i:integer;
begin
 for i:=2 to trunc(sqrt(x)) do 
  begin
   if x mod i =0 then TinhTongUoc:=TinhTongUoc+i;
  end;
end;

begin
 clrscr;
 Write('Nhap so n: ');readln(x);
 write('Cac so hoan chinh tu 1 den ',x,' la: ');
 for i:=1 to x do 
  begin
   if TinhTongUoc(i)=i then write(' ',i,',');
  end;
readln
end.
Sai rồi. Xét ước đúng là chỉ cần xét tới [sqrt(x)] nhưng khi xét i thì phải xét cả x div i.
 
Top Bottom