vilon
2007-12-09 03:00:57
I was thinking about
perfect numbers last night and made a script for finding them, though it gives me a mathematical "math" error, when it tries to take the square root of 2^32 - 1. Is this some limit in SCAR? Does anyone else know how this script could support larger numbers?
program PerfectNumbers;
function Prime(Num: Integer): Boolean;
var
i, Sq: Integer;
begin
Sq := Round(Sqrt(Num));
for i := 2 to Sq do
if(Num mod i = 0)then Exit;
Result := True;
end;
function PerfectNum(Stop: Integer): Boolean;
var
Num: Integer;
begin
for Num := 2 to Stop do
if(Prime(Round(Pow(2, Num)) - 1))then
Writeln(FloatToStr(Pow(2, Num - 1) * (Pow(2, Num) - 1)));
end;
begin
PerfectNum(32);
end.
Santa_Clause
2007-12-09 06:59:18
Hmm…like, did you just copy my Prime Number Function ;)
This is mine:
function PrimeNumber(Number : Integer) : Boolean;
var
Root : Integer;
NumberFactors : Integer;
begin
Root := Round(Sqrt(Number));
for NumberFactors := 2 to Root do
if (Number mod NumberFactors) = 0 then
Exit;
Result := True;
end;
Anyways, my perfect number function:
function PrimeNumber(Number : Integer) : Boolean;
var
Root : Integer;
NumberFactors : Integer;
begin
Root := Round(Sqrt(Number));
for NumberFactors := 2 to Root do
if (Number mod NumberFactors) = 0 then
Exit;
Result := True;
end;
function GetNumberFactors(Number : Integer) : TIntegerArray;
var
I : LongInt;
Root : LongInt;
begin
SetArrayLength(Result, 1);
Result[0] := 1;
Root := Number/2;
for I := 2 to Root do
if Number mod I = 0 then
begin
SetArrayLength(Result, GetArrayLength(Result) + 1);
Result[GetArrayLength(Result) - 1] := I;
end;
end;
function PerfectNumber(Number : Integer) : Boolean;
var
NumberArray : TIntegerArray;
NumberSum : Integer;
I : Integer;
begin
if PrimeNumber(Number) then
Exit;
NumberArray := GetNumberFactors(Number);
for I := 0 to GetArrayLength(NumberArray) - 1 do
NumberSum := NumberSum + NumberArray[I];
if NumberSum = Number then
Result := True;
end;
begin
WriteLn(BoolToStr(PerfectNumber(8128)));
end.
Kevin Wolf is going to come along and own me soon :p
As for your error, it's because you need to use a LongInt, not an Integer…since integers can only go up to a certain value.
Miitchyy
2007-12-09 07:38:16
2 to the power of 32 is larger then the capabilities of an integer,
2 to the power of 31 is 2147483648 which is 1 over it xP
function Prime(Num: Extended): Boolean;
var
i, Sq: Integer;
begin
Sq := Round(Sqrt(Num));
for i := 2 to Sq do
if(round(Num) mod i = 0)then Exit;
Result := True;
end;
vilon
2007-12-09 10:38:26
Just what I was looking for, thanks :)
How large can an extended be?
R0b0t1
2007-12-09 11:24:00
Wait… Are you looking for primes or large squares?
And what could you possibly be using these numbers for… Hmmm :p
Miitchyy
2007-12-09 17:54:48
Extendeds can be WAYYYY bigger.
It only gives you the math error because you have for Blah := 2 TO theInt and the int will be over it's limit and negative now
Ps. LongInt = Integer
R0b0t1
2007-12-09 18:30:26
Yeah, if you make an integer to large it sets the negative flag :p
Done that a few times in cheat engine…. *cough*CRASHED*cough*
Santa_Clause
2007-12-09 18:36:06
Erm…extendeds have to be decimals…
Miitchyy
2007-12-09 19:32:20
var
Rawr: Extended;
begin
Rawr := 5;
Writeln(FloatToStr(Rawr));
Rawr := Sqr(Rawr);
Writeln(FloatToStr(Rawr));
Rawr := Sqrt(Rawr);
Writeln(FloatToStr(Rawr));
end.
R0b0t1
2007-12-10 17:29:09
I think he means whole numbers. eg, 10^100
Special Ed
2007-12-10 17:33:14
ShortInt: -128 / 127 [1 Byte]
Byte: 0 / 255 [1 Byte]
SmallInt: -32,768 / 32,767 [2 Bytes]
Word: 0 / 65,535 [2 Bytes]
LongWord: 0 / 4,294,967,295 [4 Bytes]
LongInt: -2,147,483,648 / 2,147,483,647 [4 Bytes]
Integer: -2,147,483,648 / 2,147,483,647 [4 Bytes]
Int64: -2^63 / 2^63 - 1 [8 Bytes]
Cardinal: 0 / 4,294,967,295 [4 Bytes]
Single: 1.5 x 10^-45 / 3.4 x 10^38 [4 Bytes]
Double: 5 x 10^-324 / 1.7 x 10^308 [8 Bytes]
Extended: 3.4 x 10^-4932 / 1.1 x 10^4932 [10 Bytes]
Currency: -922,337,203,685,477.5808 / 922,337,203,685,477.5807 [8 Bytes]
Part of a tutorial I'm writing… those are the types of number related variables as well as how many bytes they use and their max / min values. I hope this helps a bit…
vilon
2007-12-11 15:10:05
From: Special Ed
Extended: 3.4 x 10^-4932 / 1.1 x 10^4932 [10 Bytes]
Now that's one hell of a big number (the maximum)! :p