February 3rd, 2011, 03:41 PM
-
EXITCOde 215 w bgger numbers while searching for LCM
Hello,
I have a problem w buffer overflowing
I have to (logn story short (there is a huge story line behind this problem in instructions w timetravelign etc. ...)) find the least common multiplier but when I insert bigger numbers it will exit w exitcode 215 (whcih I read its an overflow issue)
Code:
program VyrocnyPles;
var I,PocetUloh,PocetProm,PrvyClen,DruhyClen,Clen1,Clen2,Vysledok,J:Longint;
begin
readln(PocetUloh);
while PocetUloh<>0 do
begin
readln(PocetProm);
for I:=2 to PocetProm do
begin
if I=2 then
begin
read(PrvyClen, DruhyClen);
Clen1:=PrvyClen;
Clen2:=DruhyClen;
repeat
if PrvyClen>DruhyClen then PrvyClen:=PrvyClen - DruhyClen;
if PrvyClen<DruhyClen then DruhyClen:= DruhyClen - PrvyClen;
until PrvyClen=DruhyClen;
Vysledok:= Clen1 * (Clen2 div PrvyClen);
end;
If I>2 then
begin
read(PrvyClen);
Clen1:=Vysledok;
Clen2:=PrvyClen;
repeat
if PrvyClen>Vysledok then PrvyClen:=PrvyClen - Vysledok;
if PrvyClen<Vysledok then Vysledok:= Vysledok - PrvyClen;
until PrvyClen=Vysledok;
Vysledok:= Clen1 * (Clen2 div PrvyClen);
end;
end;
writeln(Vysledok);
PocetUloh:=PocetUloh - 1;
end;
readln;
end.
I jsut realized that I was giving a slvoak names to my variables so that may be a problem for u guys
Vysledok= Solution
Clen= Unit
PocetUloh (no. of assigments)
Well, on input I am given
in first row a no of assigments 1<n<10k
2nd row is number of people (numebrs that I will have to find the LCM)1<s<50k
3rd row are the numbers 1<r<10k
4th row is the same as 2nd one and so on
I am using an Euclyd alghorhytm to find the greatest common dividor and I divide 2nd unit with it and then I multiply it with the first unit and it will find me the least common multiplier for 2 numbers, then I will remember the soultion of that and then Solution= Unit 1 and I repeat the process
All works fien w small numbers, w I addd bigger numebrs it fails
Any advise? How can I overcome this without learning a new language? 
Thnx guys
February 3rd, 2011, 04:33 PM
-
Can you give some specific examples of inputs that fail?
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
February 3rd, 2011, 05:11 PM
-
2
5
123 234 345 456 567
Exitcode
February 3rd, 2011, 06:39 PM
-
Yep, it looks like that result will exceed the size of a 32-bit Longint. You can check if your Pascal compiler supports a 64-bit data type. Otherwise you will need to find a big-number library.
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
February 4th, 2011, 01:51 AM
-
Yea
but the funny thing is that libraries are forbidden from the university
And yea, I thought the same that some numbers will be just too high for long int