Pereiti prie turinio

Labai didelis skaičius


Rekomenduojami pranešimai

Sveiki. reikia Jūsų pagalbos.

 

Štai užduotis:

 

The prime factors of 13195 are 5, 7, 13 and 29.

 

What is the largest prime factor of the number 600851475143 ?

 

Mano paties kodas:

 

program Bevardė99;

var

sk,

dal

: longint;

begin

sk := 600851475143;

dal := 2;

while sk > 1 do // Kol sk didesnis už 1 vyksta ciklas

begin

if sk < dal then // tikrinama ar sk nėra mažesnis už daliklį

begin

dal := sk * (dal-1); // jei sąlyga tenkinama, daliklis sumažinamas vienetu ir surandamas dižiausias galimas daliklis

sk := 1; // ir sk = 1 ,kad sustabdytų ciklą.

end

else if sk mod dal = 0 // Tikrinama ar sk dalinasi be liekanos

then if sk = dal // Jei preiš tai sąlyga buvo teisinga, dabar tikrina ar sk ir daliklis nėra lygus

then sk := 1 // Jei teisinga sk priskiriama 1 ir dižiausias daliklis yra surastas

else // Jei nėra lygus tada surandama nauja sk reikšmė, daliklis padidinamas vienetu

begin

sk := sk div dal;

dal := dal + 1;

end

else dal := dal + 1; // Jei visos kitos sąlygos neteisingos

end;

WriteLn(dal); // Išvedama didžiausias galimas daliklis

Readln;

end.

Redagavo audrius159
Nuoroda į pranešimą
Dalintis kituose puslapiuose

gali panaudoti jau sukurtą pascal ADT:

 

{
Andrius Jaškauskas
Deividas Jankauskas
VU MIF, PS 4
2012-02
}

unit ADT_LongInt;

Interface
type LongIntType = ^LongIntT;
	 LongIntT = record
					number :integer;
					before, next :LongIntType;
				end;

procedure SetNumber (var a :LongIntType; const s :string; var err :integer);
	//skaiciu eilute pavercia i ilga sveika skaiciu
	//negiami skaiciai prasideda minuso zenklu ("-")
	//err:
		//1 - nekorektiska skaiciu eilute, ("+", ar kitu spec simboliu rasyti negalima)

procedure SetString (const a :LongIntType; var s :string; var err :integer);
	//ilga sveika skaiciu pavercia skaiciu eilute
	//err:
		//1 - skaitmuo per ilgas (>255)

function CompareNumber (const a, b :LongIntType) :integer;
	//palygina skaicius
		// 1 - (a > b)
		// 0 - (a = b)
		//-1 - (a < b)

procedure Module (var a :LongIntType);
	//skaiciu vercia teigiamu (modulis)

procedure DestroyNumber (var a :LongIntType);
	//sunaikina skaiciu, isvalo atminti

//-----------------------------------------------------------------//
//-----------------Pagrindines aritmetines operacijos--------------//
//-----------------------------------------------------------------//
function IAdd (const a, b :LongIntType) :LongIntType;
	//dvieju skaiciu suma (a + b)

function ISub (const a, b :LongIntType) :LongIntType;
	//dvieju skaiciu atimtis (a - b)

function IMul (const a, b :LongIntType) :LongIntType;
	//dvieju skaiciu daugyba (a * b)

procedure IDiv (const a, b :LongIntType; var quot, rem :LongIntType; var err :integer);
	//dvieju skaiciu dalyba (a div b = quot ir a mod b = rem); quot - dalmuo; rem - liekana
	//err:
		//1 - dalyba is 0
Implementation
type Numbers = set of '0'..'9';
const max = 255;

procedure SetNumber(var a :LongIntType; const s :string; var err :integer);	
var Tmp :LongIntType;
	i, si, n, start_at :integer;
	Sign :boolean;
	sTmp :string;
begin
	sTmp := s;
	n := length(sTmp);
	err := 0;
	Tmp := NIL;
	Sign := FALSE;

	//zenklo nustatymas
	if sTmp[1] = '-' then
	begin
		start_at := 2;
		Sign := TRUE;
		if n = 1 then err := 1;
	end
	else start_at := 1;

	//nuliu nuemimas nuo skaiciu eilutes priekio
	i := start_at;
	while ((sTmp[i] = '0') and (i < n)) do
	begin
		if sTmp[i] = '0' then start_at := start_at + 1;
		i := i + 1;
	end;

	for i := start_at to n do
	begin
		if not (sTmp[i] in Numbers) then
		begin
			err := 1;
			a := NIL;
			//i := n;
			break;
		end
		else
		begin
			new(a);
			val(sTmp[i],si);
			a^.number := si;

			if i = start_at then
			begin
				a^.before := NIL;
				a^.next := NIL;
				if Sign then a^.number := a^.number * -1;
				Tmp := a;
			end
			else
			begin
				a^.before := Tmp;
				a^.next := NIL;
				Tmp^.next := a;
				Tmp := a;
			end;
		end;
	end;
end;

procedure SetString(const a :LongIntType; var s :string; var err :integer);
var si :string;
	sk :integer;
	aTmp :LongIntType;
begin
	aTmp := a;
	sk := 0;
	err := 0;
	s := '';

	if aTmp <> NIL then while aTmp^.before <> NIL do aTmp := aTmp^.before;

	while aTmp <> NIL do
	begin			
		sk := sk + 1;
		if sk > max then
		begin
			aTmp := NIL;
			s := '';
			err := 1;
			break;
		end
		else
		begin
			if aTmp^.number < 0 then sk := sk + 1;
			Str(aTmp^.number,si);
			s := s + si;
		end;
		aTmp := aTmp^.next;
	end;
end;

procedure ExchangeOp (var a, b :LongIntType);
var c :LongIntType;
begin
	c := b;
	b := a;
	a := c;
end;

procedure Module (var a :LongIntType);
begin
	if a <> NIL then
	begin
		while a^.before <> NIL do a := a^.before;
		if a^.number < 0 then a^.number := a^.number * -1;
		while a^.next <> NIL do a := a^.next;
	end;
end;

procedure DisposeZero (var a :LongIntType);
var Tmp :LongIntType;
begin
	if a <> NIL then
	begin
		while a^.before <> NIL do a := a^.before;

		while ((a^.next <> NIL) and (a^.number = 0)) do
		begin
			Tmp := a;
			a := a^.next;
			a^.before := NIL;
			Dispose(Tmp);
		end;

		while a^.next <> NIL do a := a^.next;
	end;
end;

procedure DestroyNumber (var a :LongIntType);
var Tmp :LongIntType;
begin
	if a <> NIL then
	begin
		while a^.next <> NIL do a := a^.next;

		while a <> NIL do
		begin
			Tmp := a;
			a := a^.before;
			Dispose(Tmp);
		end;
	end;
end;


function CompareNumber (const a, b :LongIntType) :integer;
var aTmp, bTmp :LongIntType;
	T, Sign :boolean;
	err, nr1, nr2 :integer;
begin
	//rez = -1 => a < b
	//rez =  0 => a = b
	//rez =  1 => a > b

	Sign := FALSE;
	aTmp := a;
	bTmp := b;
	T := FALSE;
	CompareNumber := 0;

	if aTmp = NIL then SetNumber(aTmp,'0',err);
	if bTmp = NIL then SetNumber(bTmp,'0',err);

	while aTmp^.before <> NIL do aTmp := aTmp^.before;
	while bTmp^.before <> NIL do bTmp := bTmp^.before;
	nr1 := aTmp^.number;
	nr2 := bTmp^.number;

	if ((aTmp^.number < 0) and (bTmp^.number < 0)) then Sign := TRUE;

	if ((aTmp^.number >= 0) and (bTmp^.number < 0)) then
	begin
		T := TRUE;
		CompareNumber := 1;
	end;

	if ((aTmp^.number < 0) and (bTmp^.number >= 0)) then
	begin
		T := TRUE;
		CompareNumber := -1;
	end;

	Module(aTmp);
	Module(bTmp);

	if ((aTmp <> NIL) and (bTmp <> NIL) and not T) then
	begin
		while ((aTmp^.before <> NIL) and (bTmp^.before <> NIL) and not T) do
		begin
			aTmp := aTmp^.before;
			bTmp := bTmp^.before;

			if ((aTmp^.before = NIL) and (bTmp^.before <> NIL)) then
			begin
				T := TRUE;
				CompareNumber := -1;
			end;

			if ((bTmp^.before = NIL) and (aTmp^.before <> NIL)) then
			begin
				T := TRUE;
				CompareNumber := 1
			end;
		end;

		//jei a vienzenklis
		if ((aTmp^.before = NIL) and (aTmp^.next = NIL) and (bTmp^.before <> NIL)) then
		begin
			T := TRUE;
			CompareNumber := -1;
		end;

		//jei b vienzenklis
		if ((bTmp^.before = NIL) and (bTmp^.next = NIL) and (aTmp^.before <> NIL)) then
		begin
			T := TRUE;
			CompareNumber := 1;
		end;

		while aTmp^.before <> NIL do aTmp := aTmp^.before;
		while bTmp^.before <> NIL do bTmp := bTmp^.before;

		//jei skaiciu ilgiai lygus
		if not T then
		begin
			while ((aTmp^.next <> NIL) and (bTmp^.next <> NIL) and not T) do
			begin
				if aTmp^.number < bTmp^.number then
				begin
					T := TRUE;
					CompareNumber := -1;
				end;

				if aTmp^.number > bTmp^.number then
				begin
					T := TRUE;
					CompareNumber := 1;
				end;

				aTmp := aTmp^.next;
				bTmp := bTmp^.next;
			end;

			if ((aTmp^.next = NIL) and (bTmp^.next = NIL) and not T) then
				if aTmp^.number < bTmp^.number then
				begin
					T := TRUE;
					CompareNumber := -1;
				end
				else if aTmp^.number > bTmp^.number then
				begin
					T := TRUE;
					CompareNumber := 1;
				end;
		end;

	end;
	while aTmp^.before <> NIL do aTmp := aTmp^.before;
	while bTmp^.before <> NIL do bTmp := bTmp^.before;
       aTmp^.number := nr1;
       bTmp^.number := nr2;
	if Sign then CompareNumber := CompareNumber * -1;

	while aTmp^.before <> NIL do aTmp := aTmp^.before;
	while bTmp^.before <> NIL do bTmp := bTmp^.before;
end;

function AddAlg (const a, b :LongIntType) :LongIntType;
var aTmp, bTmp, Tmp1, Tmp2 :LongIntType;
	mintis, err :integer;
begin
	aTmp := a;
	bTmp := b;

	if aTmp = NIL then SetNumber(aTmp,'0',err);
	if bTmp = NIL then SetNumber(bTmp,'0',err);

	mintis := 0;
       new(Tmp1);
       Tmp1^.next := NIL;
       Tmp1^.before := NIL;
	while (bTmp^.before <> NIL) do
	begin
		Tmp1^.number := aTmp^.number + bTmp^.number + mintis;
		if Tmp1^.number > 9 then
		begin
			Tmp1^.number := Tmp1^.number - 10;
			mintis := 1;
		end
		else mintis := 0;
		aTmp := aTmp^.before;
		bTmp := bTmp^.before;
		new(Tmp2);
           Tmp2^.next := Tmp1;
           Tmp2^.before := NIL;
           Tmp1^.before := Tmp2;
		Tmp1 := Tmp1^.before;
	end;
	Tmp1^.number := aTmp^.number + bTmp^.number + mintis;
	mintis := 0;
	if Tmp1^.number > 9 then
	begin
		Tmp1^.number := Tmp1^.number - 10;
		mintis := 1;
	end;
	if (aTmp^.before = NIL) then
	begin
		new (bTmp);
		bTmp^.number := mintis;
		bTmp^.next := Tmp1;
		bTmp^.before := NIL;
		Tmp1^.before := bTmp;
		Tmp1 := Tmp1^.before;
	end
	else if aTmp^.before <> NIL then
	begin
		aTmp := aTmp^.before; 
           new(Tmp2);
           Tmp2^.next := Tmp1;
           Tmp2^.before := NIL;
           Tmp1^.before := Tmp2;
		Tmp1 := Tmp1^.before;
		while aTmp^.before <> NIL do
		begin
			Tmp1^.number := aTmp^.number + mintis;
			if Tmp1^.number > 9 then
			begin
				Tmp1^.number := Tmp1^.number - 10;
				mintis := 1;
			end
			else mintis := 0;
			aTmp := aTmp^.before;
		new(Tmp2);
           Tmp2^.next := Tmp1;
           Tmp2^.before := NIL;
           Tmp1^.before := Tmp2;
		Tmp1 := Tmp1^.before;
		end;

		Tmp1^.number := aTmp^.number + mintis;
		mintis := 0;
		if Tmp1^.number > 9 then
		begin
			Tmp1^.number := Tmp1^.number - 10;
			mintis := 1;
		end;

		if ((aTmp^.before = NIL) and  (mintis = 1)) then
		begin
			new (bTmp);
			bTmp^.number := mintis;
			bTmp^.next := Tmp1;
			bTmp^.before := NIL;
			Tmp1^.before := bTmp;
			Tmp1 := Tmp1^.before;
		end;
	end;

	AddAlg := Tmp1;
end;

function SubAlg (const a, b :LongIntType) :LongIntType;
var aTmp, bTmp, Tmp1, Tmp2 :LongIntType;
	mintis, err :integer;
begin
	aTmp := a;
	bTmp := b;

	if aTmp = NIL then SetNumber(aTmp,'0',err);
	if bTmp = NIL then SetNumber(bTmp,'0',err);

	mintis := 0;
       new(Tmp1);
       Tmp1^.next := NIL;
       Tmp1^.before :=NIL;
	while (bTmp^.before <> NIL)  do
	begin
		Tmp1^.number := aTmp^.number - bTmp^.number - mintis; 
		if Tmp1^.number < 0 then
		begin
			Tmp1^.number := Tmp1^.number + 10;
			mintis := 1;
		end
		else mintis := 0;
		aTmp := aTmp^.before;
		bTmp := bTmp^.before;
		new(Tmp2);
           Tmp2^.next := Tmp1;
           Tmp2^.before := NIL;
           Tmp1^.before := Tmp2;
		Tmp1 := Tmp1^.before;
	end;

	Tmp1^.number := aTmp^.number - bTmp^.number - mintis;
	mintis := 0;

	if Tmp1^.number < 0 then
	begin
		Tmp1^.number := Tmp1^.number + 10;
		mintis := 1;
	end;

	if (aTmp^.before = NIL) and (mintis = 1) then
	begin
		new(bTmp);
		bTmp^.number := mintis;
		bTmp^.next := Tmp1;
		bTmp^.before := NIL;
		Tmp1^.before := bTmp;
		Tmp1 := Tmp1^.before;
	end
	else if aTmp^.before <> NIL then
	begin
		aTmp := aTmp^.before;
		new(Tmp2);
           Tmp2^.next := Tmp1;
           Tmp2^.before := NIL;
           Tmp1^.before := Tmp2;
		Tmp1 := Tmp1^.before;
		while aTmp^.before <> NIL do
		begin
			Tmp1^.number := aTmp^.number - mintis;
			if Tmp1^.number < 0 then
			begin
				Tmp1^.number := Tmp1^.number + 10;
				mintis := 1;
			end
			else mintis := 0;
			aTmp := aTmp^.before;
	  	    new(Tmp2);
               Tmp2^.next := Tmp1;
               Tmp2^.before := NIL;
               Tmp1^.before := Tmp2;
               Tmp1 := Tmp1^.before;
		end;

		Tmp1^.number := aTmp^.number - mintis;
		mintis := 0;
		if Tmp1^.number < 0 then
		begin
			Tmp1^.number := Tmp1^.number + 10;
			mintis := 1;
		end;
		if (a^.before = NIL) and  (mintis = 1) then
		begin
			new (bTmp);
			bTmp^.number := mintis;
			bTmp^.next := Tmp1;
			bTmp^.before := NIL;
			Tmp1^.before := bTmp;
			Tmp1 := Tmp1^.before;
		end;
	end;

	SubAlg := Tmp1;
end;

procedure DivAlg (const a, b :LongIntType; var c, d :LongIntType; var err :integer);
var vienetukas, aTmp, bTmp :LongIntType;
	T :boolean;
	i :integer;
begin
	aTmp := a;
	bTmp := b;
	SetNumber(c,'0',err);
	SetNumber(d,'0',err);
	if aTmp = NIL then SetNumber(aTmp,'0',err);
	if bTmp = NIL then SetNumber(bTmp,'0',err);

	DisposeZero(aTmp);
	DisposeZero(bTmp);
	Module(aTmp);
	Module(bTmp);

	i := CompareNumber(aTmp,bTmp);
	if ((i = 1) or (i = 0)) then T := TRUE
							else T := FALSE;

	if ((bTmp^.before = NIL) and (bTmp^.next = NIL) and (bTmp^.number = 0)) then
	begin
		c := NIL;
		d := NIL;
		err := 1;
	end
	else if ((aTmp^.before = NIL) and (aTmp^.next = NIL) and (aTmp^.number = 0)) then 
	begin
		SetNumber(c,'0',err);
		SetNumber(d,'0',err);
	end	
	else
	begin
		while T do
		begin
			aTmp := SubAlg(aTmp,bTmp);
			SetNumber(vienetukas,'1',err);
			c := AddAlg(c,vienetukas);
			DisposeZero(aTmp);
			DisposeZero(c);
			i := CompareNumber(aTmp,bTmp);
			if ((i = 1) or (i = 0)) then T := TRUE
									else T := FALSE;
		end;
		while aTmp^.next <> NIL do aTmp := aTmp^.next;
           d^.number := aTmp^.number;
           while aTmp^.before <> NIL do
	  	begin
               aTmp :=aTmp^.before;
               new(bTmp);
               bTmp^.before := NIL;
               bTmp^.next := d;
               d^.before := bTmp;
               d := d^.before;
               d^.number := aTmp^.number;
		end;

	end;
	DisposeZero(d);
	DisposeZero(c);

	if c <> NIL then while c^.next <> NIL do c := c^.next;
	if d <> NIL then while d^.next <> NIL do d := d^.next;
end;

procedure AddZeroes (var a, n :LongIntType);
var Tmp, vienetukas, n2 :LongIntType;
	err :integer;
begin
	SetNumber(n2,'0',err);

	if ((a <> NIL) and (n <> NIL)) then
	begin
		while a^.next <> NIL do a := a^.next;

		while ((n^.before <> NIL) or (n^.number > 0)) do
		begin
			SetNumber(vienetukas,'1',err);
			n := SubAlg(n,vienetukas);
			DisposeZero(n);

			SetNumber(vienetukas,'1',err);
			n2 := AddAlg(n2,vienetukas);
			DisposeZero(n2);

			new(Tmp);
			Tmp^.number := 0;
			Tmp^.next := NIL;
			Tmp^.before := a;
			a^.next := Tmp;
			a := Tmp;
		end;
		n := n2;
	end;
end;

function IAdd (const a, b :LongIntType) :LongIntType;
var aTmp, bTmp :LongIntType;
	Size, err :integer;
	Sign, S1, S2 :boolean;
begin
	aTmp := a;
	bTmp := b;

	if aTmp = NIL then SetNumber(aTmp,'0',err);
	if bTmp = NIL then SetNumber(bTmp,'0',err);

	S1 := FALSE;
	S2 := FALSE;

	while aTmp^.before <> NIL do aTmp := aTmp^.before;
	while bTmp^.before <> NIL do bTmp := bTmp^.before;

	if aTmp^.number < 0 then S1 := TRUE;
	if bTmp^.number < 0 then S2 := TRUE;

	while aTmp^.next <> NIL do aTmp := aTmp^.next;
	while bTmp^.next <> NIL do bTmp := bTmp^.next;


	Module(aTmp);
	Module(bTmp);
	DisposeZero(aTmp);
	DisposeZero(bTmp);

	while aTmp^.next <> NIL do aTmp := aTmp^.next;
	while bTmp^.next <> NIL do bTmp := bTmp^.next;
	Size := CompareNumber(aTmp, bTmp);

	if (not S1 and not S2) then
	begin
		Sign := FALSE;
		if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
		IAdd := AddAlg(aTmp, bTmp);
	end;

	if (S1 and S2) then
	begin
		Sign := TRUE;
		if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
		IAdd := AddAlg(aTmp, bTmp);
	end;

	if (not S1 and S2 and ((Size = 0) or (Size = 1))) then
	begin
		Sign := FALSE;
		if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
		IAdd := SubAlg(aTmp,bTmp);
	end;

	if (not S1 and S2 and (Size = -1)) then
	begin
	//writeln('a');
		Sign := TRUE;
		if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
		IAdd := SubAlg(aTmp,bTmp);
	end;

	if (S1 and not S2 and ((Size = 0) or (Size = 1))) then
	begin
		Sign := TRUE;
		if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
		IAdd := SubAlg(aTmp,bTmp);
	end;

	if (S1 and not S2 and (Size = -1)) then
	begin
		Sign := FALSE;
		if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
		IAdd := SubAlg(aTmp,bTmp);
	end;

	DisposeZero(IAdd);
	if Sign then
	begin
		while IAdd^.before <> NIL do IAdd := IAdd^.before;
		IAdd^.number := IAdd^.number * -1;
	end;

	while IAdd^.next <> NIL do IAdd := IAdd^.next;
end;

function ISub (const a, b :LongIntType) :LongIntType;
var aTmp, bTmp :LongIntType;
	Size, err :integer;
	Sign, S1, S2 :boolean;
begin
	aTmp := a;
	bTmp := b;

	if aTmp = NIL then SetNumber(aTmp,'0',err);
	if bTmp = NIL then SetNumber(bTmp,'0',err);

	S1 := FALSE;
	S2 := FALSE;

	while aTmp^.before <> NIL do aTmp := aTmp^.before;
	while bTmp^.before <> NIL do bTmp := bTmp^.before;

	if aTmp^.number < 0 then S1 := TRUE;
	if bTmp^.number < 0 then S2 := TRUE;

		while aTmp^.next <> NIL do aTmp := aTmp^.next;
	while bTmp^.next <> NIL do bTmp := bTmp^.next;


	Module(aTmp);
	Module(bTmp);
	DisposeZero(aTmp);
	DisposeZero(bTmp);

	while aTmp^.next <> NIL do aTmp := aTmp^.next;
	while bTmp^.next <> NIL do bTmp := bTmp^.next;
	Size := CompareNumber(aTmp, bTmp);

	if (not S1 and not S2 and ((Size = 0) or (Size = 1))) then
		begin
			Sign := FALSE;
			if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
			ISub := SubAlg(aTmp,bTmp);
		end;

		if (not S1 and not S2 and (Size = -1)) then
		begin
			Sign := TRUE;
			if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
			ISub := SubAlg(aTmp,bTmp);
		end;

		if (S1 and S2 and ((Size = 0) or (Size = 1))) then
		begin
			Sign := TRUE;
			if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
			ISub := SubAlg(aTmp,bTmp);
		end;

		if (S1 and S2 and (Size = -1)) then
		begin
			Sign := FALSE;
			if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
			ISub := SubAlg(aTmp,bTmp);
		end;

		if (not S1 and S2) then
		begin
			Sign := FALSE;
			if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
			ISub := AddAlg(aTmp,bTmp);
		end;

		if (S1 and not S2) then
		begin
			Sign := TRUE;
			if CompareNumber(aTmp, bTmp) = -1 then ExchangeOp(aTmp, bTmp);
			ISub := AddAlg(aTmp,bTmp);
		end;

	DisposeZero(ISub);

	if Sign then
	begin
		while ISub^.before <> NIL do ISub := ISub^.before;
		ISub^.number := ISub^.number * -1;
	end;

	while ISub^.next <> NIL do ISub := ISub^.next;
end;

function IMul (const a, b :LongIntType) :LongIntType;
var Tmp1, Tmp2, Tail, vienetukas, aTmp, bTmp :LongIntType;
	First, Sign, S1, S2 :boolean;
	mintis, n, err, i :integer;
begin
	aTmp := a;
	bTmp := b;

	if aTmp = NIL then SetNumber(aTmp,'0',err);
	if bTmp = NIL then SetNumber(bTmp,'0',err);

	S1 := FALSE;
	S2 := FALSE;
	mintis := 0;
	First := TRUE;

	Tmp1 := NIL;
	Tmp2 := NIL;

	while aTmp^.before <> NIL do aTmp := aTmp^.before;
	while bTmp^.before <> NIL do bTmp := bTmp^.before;

	if aTmp^.number < 0 then S1 := TRUE;
	if bTmp^.number < 0 then S2 := TRUE;

	while aTmp^.next <> NIL do aTmp := aTmp^.next;
	while bTmp^.next <> NIL do bTmp := bTmp^.next;

	DisposeZero(aTmp);
	DisposeZero(bTmp);
	Module(aTmp);
	Module(bTmp);

	if (not S1 and not S2) or (S1 and S2) then Sign := FALSE;
	if (not S1 and S2) or (S1 and not S2) then Sign := TRUE;

	SetNumber(Tail,'0',err);
	SetNumber(IMul,'0',err);

	while aTmp <> NIL do
	begin
		while bTmp <> NIL do
		begin
			n := aTmp^.number * bTmp^.number + mintis;
			mintis := 0;
			if n > 9 then
			begin
				mintis := n div 10;
				n := n mod 10;
			end;

			if Tmp1 = NIL then
			begin
				new(Tmp1);
				Tmp1^.number := n;
				Tmp1^.next := NIL;
				Tmp1^.before := NIL;
			end
			else
			begin
				new(Tmp1);
				Tmp1^.number := n;
				Tmp1^.next := Tmp2;
				Tmp1^.before := NIL;
				Tmp2^.before := Tmp1;
			end;

			Tmp2 := Tmp1;
			bTmp := bTmp^.before;

			if ((bTmp = NIL) and (mintis > 0)) then
			begin
				while Tmp1^.before <> NIL do Tmp1 := Tmp1^.before;
				new(Tmp2);
				Tmp2^.number := mintis;
				Tmp2^.before := NIL;
				Tmp2^.next := Tmp1;
				Tmp1^.before := Tmp2;
				mintis := 0;
			end;
		end;

		if not First then
		begin
			SetNumber(vienetukas,'1',err);
			Tail := AddAlg(Tail,vienetukas);
			DisposeZero(Tail);
			AddZeroes(Tmp1,Tail);

			while IMul^.next <> NIL do IMul := IMul^.next;
			i := CompareNumber(IMul,Tmp1);
			if i = -1 then ExchangeOp(IMul,Tmp1);
			IMul := AddAlg(IMul,Tmp1);
			DisposeZero(IMul);
		end
		else
		begin
			First := FALSE;
			IMul := Tmp1;
		end;

		Tmp1 := NIL;

		bTmp := b;
		aTmp := aTmp^.before;
	end;

	DisposeZero(IMul);
	if (Sign and (IMul <> NIL)) then
	begin
		while IMul^.before <> NIL do IMul := IMul^.before;
		IMul^.number := IMul^.number * -1;
	end;

	if IMul <> NIL then while IMul^.next <> NIL do IMul := IMul^.next;
end;

procedure IDiv (const a, b :LongIntType; var quot, rem :LongIntType; var err :integer);
var aTmp, bTmp, Tmp1, Tmp2, quotientTmp, remainderTmp :LongIntType;
	Sign1, Sign2, S1, S2, First :boolean;
begin
	aTmp := a;
	bTmp := b;

	if aTmp = NIL then SetNumber(aTmp,'0',err);
	if bTmp = NIL then SetNumber(bTmp,'0',err);

	S1 := FALSE;
	S2 := FALSE;

	while aTmp^.before <> NIL do aTmp := aTmp^.before;
	while bTmp^.before <> NIL do bTmp := bTmp^.before;

	if aTmp^.number < 0 then S1 := TRUE;
	if bTmp^.number < 0 then S2 := TRUE;

	while aTmp^.next <> NIL do aTmp := aTmp^.next;
	while bTmp^.next <> NIL do bTmp := bTmp^.next;

	DisposeZero(aTmp);
	DisposeZero(bTmp);
	Module(aTmp);
	Module(bTmp);

	if (not S1 and not S2) or (S1 and S2) then Sign1 := FALSE;
	if (not S1 and S2) or (S1 and not S2) then Sign1 := TRUE;
       Sign2 := S1;

	SetNumber(quot,'0',err);
	SetNumber(rem,'0',err);

	First := TRUE;

	if ((bTmp^.before = NIL) and (bTmp^.next = NIL) and (bTmp^.number = 0)) then
	begin
		quot := NIL;
		rem := NIL;
		err := 1;
	end
	else
	begin
		while aTmp <> NIL do
		begin
			if First then
			begin
				while aTmp^.before <> NIL do aTmp := aTmp^.before;
				Tmp1 := aTmp;
				aTmp := aTmp^.next;
				if aTmp <> NIL then aTmp^.before := NIL;
				Tmp1^.next := NIL;
				First := FALSE;
			end
			else
			begin
				while aTmp^.before <> NIL do aTmp := aTmp^.before;
				while Tmp1^.next <> NIL do Tmp1 := Tmp1^.next;
				Tmp2 := aTmp;
				aTmp := aTmp^.next;
				if aTmp <> NIL then aTmp^.before := NIL;
				Tmp1^.next := Tmp2;
				Tmp2^.before := Tmp1;
				Tmp2^.next := NIL;
				Tmp1 := Tmp1^.next;
			end;

			DivAlg(Tmp1,bTmp,quotientTmp,remainderTmp,err);
			while quotientTmp^.before <> NIL do quotientTmp := quotientTmp^.before;
			while remainderTmp^.before <> NIL do remainderTmp := remainderTmp^.before;
			while quot^.next <> NIL do quot := quot^.next;

			quot^.next := quotientTmp;
			quotientTmp^.before := quot;
			DisposeZero(quot);

			rem := remainderTmp;
			Tmp1 := remainderTmp;
			if ((remainderTmp^.number = 0) and (remainderTmp^.next = NIL) and (remainderTmp^.before = NIL)) then First := TRUE;
		end;
	end;
	DisposeZero(rem);
	DisposeZero(quot);

	if (Sign1 and (quot <> NIL)) then
	begin
		while quot^.before <> NIL do quot := quot^.before;
		quot^.number := quot^.number * -1;
	end;
	if (Sign2 and (rem <> NIL)) then
	begin
		while rem^.before <> NIL do rem := rem^.before;
		rem^.number := rem^.number * -1;
	end;

	if quot <> NIL then while quot^.next <> NIL do quot := quot^.next;
	if rem <> NIL then while rem^.next <> NIL do rem := rem^.next;
end;

end.

Nuoroda į pranešimą
Dalintis kituose puslapiuose

Meta klaidas dėl tipo.

 

Incompatibles types: got "S80REAL" expected "INT64"

 

Tas pats ir su longint.

 

Gali būti, kad kompiliatorius nesupranta:

It is to note that the qword and int64 types are not true ordinals, so some pascal constructs will not work with these two integer types.

 

Pabandyk pasileisti šitą pavyzdį:

(***************************************************************
* File:        Integer2.pas
* Language:    Pascal
* Description: This program has examples of
*              integer literal constants.
***************************************************************)

PROGRAM Integer2 (OUTPUT);

VAR
 Count: ShortInt;
 LongCount: LongInt;
 Count64: Int64;

BEGIN
 WriteLn ('Pascal Integer Examples');
 Count := 21;
 WriteLn ('Count has a value of ', Count);
 LongCount := -21234567;
 WriteLn ('LongCount has a value of ', LongCount);
 LongCount := 86512;
 WriteLn ('Count has a value of ', LongCount);
 Count64 := 33720368547758;
 WriteLn ('Count64 has a value of ', Count64);
END.

 

 

 

Real naudok :)

 

Realiųjų skaičių reikia vengti kaip velnias kryžiaus :) Juos naudoti tik tada, kai užduotis reikalauja realaus skaičiaus.

Nuoroda į pranešimą
Dalintis kituose puslapiuose

Gali būti, kad kompiliatorius nesupranta:

 

 

Pabandyk pasileisti šitą pavyzdį:

(***************************************************************
* File:        Integer2.pas
* Language:    Pascal
* Description: This program has examples of
*              integer literal constants.
***************************************************************)

PROGRAM Integer2 (OUTPUT);

VAR
 Count: ShortInt;
 LongCount: LongInt;
 Count64: Int64;

BEGIN
 WriteLn ('Pascal Integer Examples');
 Count := 21;
 WriteLn ('Count has a value of ', Count);
 LongCount := -21234567;
 WriteLn ('LongCount has a value of ', LongCount);
 LongCount := 86512;
 WriteLn ('Count has a value of ', LongCount);
 Count64 := 33720368547758;
 WriteLn ('Count64 has a value of ', Count64);
END.

 

 

Realiųjų skaičių reikia vengti kaip velnias kryžiaus :) Juos naudoti tik tada, kai užduotis reikalauja realaus skaičiaus.

 

 

Prie eilutes Count64 := .... meta vėl ta pačia klaidą.

Incompatible types: got "S80REAL" expected "INT64"

Redagavo audrius159
Nuoroda į pranešimą
Dalintis kituose puslapiuose

Prie unit meta klaida: Illegal unit name: ADT_LONGINT

 

Jeigu kas galite, padekite ja pasinaudoti, nes man dar tokie dalykai suvokti per sunkus.

Ar kaip kitaip išspręsti šią problemą.

 

Pateikiu pavyzdį, kaip naudojamas šis ADT

 

program ilgasSk;

uses ADT_LongInt; //visa ta unitą išsaugok vardu: ADT_LongInt.pas ir laikyk tame pačiame aplanke, kaip ir ši programa

var S1, S2, R1, R2, op :string;
LI1, LI2, LI_r1, LI_r2 :LongIntType;
er1, er2 :integer;
begin
   er1 := 0;
   er2 := 0;

op := '*'; 	//op = {+,-,*,/}
S1 := '150';
S2 := '-25';

//is string tipo skaiciu sukuriami LongIntType tipo skaiciai
SetNumber(LI1,S1,er1);
SetNumber(LI2,S2,er2);

//(1) = (>); (0) = (=); (-1) = (<)
WriteLn('Palyginimo rezultatas: ',CompareNumber(LI1,LI2));
WriteLn;

if ((er1 = 1) or (er2 = 1)) then WriteLn('Klaida! "String" tipo skaicius ivestas nekorektiskai. ("+" rasyti nereikia).')
else
begin
	er1 := 0;
	er2 := 0;

	if op = '+' then LI_r1 := IAdd(LI1,LI2); //dvieju LongIntType tipo skaiciu suma
	if op = '-' then LI_r1 := ISub(LI1,LI2); //dvieju LongIntType tipo skaiciu atimtis
	if op = '*' then LI_r1 := IMul(LI1,LI2); //dvieju LongIntType tipo skaiciu daugyba

	if ((op = '+') or (op = '-') or (op = '*')) then
	begin
		SetString(LI_r1,R1,er1); //LongIntType tipo skaicius verciamas i string tipo skaiciu
		if er1 = 1 then WriteLn('Klaida! "LongIntType" tipo skaicius per ilgas. Ilgis virsija 255.')
		else
		begin
			WriteLn('Rezultatas: ',R1);

			DestroyNumber(LI_r1); //sunaikinamas LongIntType tipo skaicius, atlaisvinama atmintis
		end;
	end;

	if op = '/' then
	begin
		IDiv(LI1,LI2,LI_r1,LI_r2,er1); //dvieju LongIntType tipo skaiciu dalyba

		if er1 = 1 then WriteLn('Klaida! Ivyko dalyba is nulio.')
		else
		begin
			er1 := 0;
			SetString(LI_r1,R1,er1);
			SetString(LI_r2,R2,er2);

			if ((er1 = 1) or (er2 = 1)) then WriteLn('Klaida! "LongIntType" tipo skaicius per ilgas. Ilgis virsija 255.')
			else
			begin
				WriteLn('Rezultatas: ',R1);
				WriteLn('Liekana: ',R2);

				SetString(LI1,R1,er1);
				SetString(LI2,R2,er1);
				WriteLn('a: ',R1);
				WriteLn('b: ',R2);

				DestroyNumber(LI_r1);
				DestroyNumber(LI_r2);
			end;
		end;
	end;
end;

DestroyNumber(LI1);
DestroyNumber(LI2);

ReadLn;
end.

Nuoroda į pranešimą
Dalintis kituose puslapiuose

Prisijunkite prie diskusijos

Jūs galite rašyti dabar, o registruotis vėliau. Jeigu turite paskyrą, prisijunkite dabar, kad rašytumėte iš savo paskyros.

Svečias
Parašykite atsakymą...

×   Įdėta kaip raiškusis tekstas.   Atkurti formatavimą

  Only 75 emoji are allowed.

×   Nuorodos turinys įdėtas automatiškai.   Rodyti kaip įprastą nuorodą

×   Jūsų anksčiau įrašytas turinys buvo atkurtas.   Išvalyti redaktorių

×   You cannot paste images directly. Upload or insert images from URL.

Įkraunama...
  • Dabar naršo   0 narių

    Nei vienas registruotas narys šiuo metu nežiūri šio puslapio.

×
×
  • Pasirinkite naujai kuriamo turinio tipą...