SRL Archive - Other Scripts

Prime Finder by Ron


Ron

2008-03-08 12:54:31

Just a simple script I cooked up. I was bored and I saw a Prime Number related thread and I thought why not make one that is different. This one can't do 11 digit numbers because I don't know how to do that with ONLY scar and not a plugin so w/e. This is just for fun. :D

Well here you go. The file is attached. It can check a range of numbers if you set the minimum and maximum. Here is a range of 0 to 100.

1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
-- 26 prime numbers found.
-- Completed in 20 milliseconds.
Successfully executed

~Ron

Harry

2008-03-08 12:56:36

Nice work dude, rep+!

(Will this work on 11 digit primes? :))

bullzeye95

2008-03-08 12:59:00

Instead of Limit := Number / 2;, you should do Limit := Floor(Sqrt(Number)); :)

EDIT: Hey, Ron, didn't realize that was you! Nice to see a post from you :D

mixster

2008-03-08 13:25:56

My plugin's cooler and quicker… :) I currently have 15k 11 digit prime numbers in an hour or so of running it. And I use Trunc(Sqrt(number))+2 personally, but that's just me :p Wish I remember mod when writing the inital scar script though, would've made it a lot simpler. Do they have mod in delphi? Ahwell, nice script though it really should be to the Sqrt as said, makes it run much much quicker when doing larger numbers.

Edit: Changing to Sqrt took the time down from 187 ms to 125 ms (0 to 500). Will try with larger numbers and see the effect of it.
Edit2: 0 to 50000 for the normal limit takes 453719 and the Sqrt limit takes 13687, so much much faster to use Sqrt at higher numbers.

bullzeye95

2008-03-08 15:13:44

Yes, and also try it without the writelns… much much faster. Writeln really eats up the MS :p

Ron

2008-03-08 16:20:24

Hey you guys are right. Much faster. But why square root it? What's the significance of that?

I looked it up on wikipedia and the same technique was shown, but it didn't have an explanation. Maybe I'm just not seeing something that's obvious.

Edit: The WriteLns I tried before I even posted the script lol and it doesn't affect the overall speed. In the end, the speed is EXACTLY the same or near to it.

ShowerThoughts

2008-03-08 16:22:30

this means your back?

bullzeye95

2008-03-08 16:27:40

From: Ron
Hey you guys are right. Much faster. But why square root it? What's the significance of that?

I looked it up on wikipedia and the same technique was shown, but it didn't have an explanation. Maybe I'm just not seeing something that's obvious.

Edit: The WriteLns I tried before I even posted the script lol and it doesn't affect the overall speed. In the end, the speed is EXACTLY the same or near to it.


The square root works because if you go up to half of the number, then it's already divisible by 2, and therefor a prime number… if you know what I mean.

And about the writelns, I get about 92 ms with them, and 16 ms without them.

Ron

2008-03-08 16:31:51

lol not really because I'm not really interested in RS. I never really was, but before I was more into it. It's still fun to play around with scar though. I may come around every now and then to say hi though. :)

Oh and no it doesn't work with 11 digits. In the script, I'm using LongInts and those are 32-bit and can hold a maximum value of 2,147,483,647. There is another type in scar called Int64, but it doesn't seem to work in loops or anything. I'm guessing Int64 can hold 64-bit information meaning a maximum value of …mm who knows. A lot more though lol.

About the writelns. If I don't put the writelns in, you're right. It's much faster. But I still want to display the information. So if I had the numbers processed first, and then displayed, that comes out to the same time it does if I display the results while processing. I'm guessing you just take the writelns out, but then all you get is how many prime #s found and how long it took.

mixster

2008-03-08 16:40:55

The Square root method is easiest with an example. I'll use 16.
The ways to get 16 are (and I know I left a few out, but this just makes it easier when you just have whole numbers):
1x16
2x8
4x4
8x2
1x16
The square root of 16 is 4 and that has the highest UNIQUE combination, as 8x2 was already done by 2x8 and 1x16 was already done by 1x16. It also gives the lowest number when added together, but that's irrelevant.

With 64, you have (as above):
1x64
2x32
4x16
8x8
16x4 (same as 4x16)
32x2 (same as 2x32)
64x1 (same as 1x64)
Once again, the suare root of 64 (8) is the highest unique multiple combination - everything after it has already been done.

Also, check my script for how I used int64's - really pissed me off, you can't use Inc, for loops and have to do Int64ToStr otherwise you get random numbers >< but it was worth it in the end. Pretty much anything that only accepts LongInt's will probably have an error.

Harry

2008-03-08 16:49:13

158699
158731
158747
158749
158759
158761
158771
158777
[Runtime Error] : Exception: Access violation at address 00405260 in module 'scar.exe'. Write of address 747069C2 in line 62 in script C:\Documents and Settings\Harry\Desktop\PrimeFinder-v.02.scar


Was trying to find the highest prime number :(

bullzeye95

2008-03-08 19:32:28

Not to thread hijack, but I researched faster ways of finding prime numbers, and I found the Sieve of Eratosthenes… I ported it to SCAR, and, at 100000 numbers, it is about 3 seconds faster that your current method.
function GetPrimeNumbers(N: Integer): TIntegerArray;
var
I, C, CC, CN, Limit, Hi: Integer;
Arr, Arr2: TIntegerArray;
begin
Limit:= Floor(Sqrt(N));
SetLength(Arr, N - 1);
SetLength(Arr2, N - 1);
SetLength(Result, N);
for I:= 2 to N do
Arr[I - 2]:= I;
while CN <= Limit do
begin
CC:= 0;
Result[C]:= Arr[0];
inc(C);
CN:= Arr[0];
Hi:= High(Arr);
for I:= 0 to Hi do
if(Arr[I] mod CN <> 0)then
begin
Arr2[CC]:= Arr[I];
inc(CC);
end;
SetLength(Arr2, CC);
SetLength(Arr, 0);
Arr:= Arr2;
end;
dec(C);
for I:= 0 to High(Arr) do
Result[I + C]:= Arr[I];
SetLength(Result, C + I);
end;
If you want to look at the method. (Please don't mind any bad coding, I didn't spend long on it :p)

mixster

2008-03-09 06:29:16

Just when I thought I was finished with prime numbers, you had to throw in a curveball :p Anyway, I'm more interested in the Sieve of Atkin which is apparently even fatser, so will post it up after I get the script working for that.

bullzeye95

2008-03-09 12:04:29

From: mixster05
Anyway, I'm more interested in the Sieve of Atkin which is apparently even fatser, so will post it up after I get the script working for that.


Yes, I tried to do that last night, but finding the solutions to 3x^2 + y^2 = CurrentNumber and the other two is HARD. I didn't know how to do it, and gave up after an hour or so.

mixster

2008-03-09 18:16:39

I got bored after writing about 2 lines and decided to find the buzz delphi tut videos. I'm gonna make my game tomorrow and maybe finish the sieve of atkin afterwards.

smithsps

2008-03-09 19:03:08

Dang it I was going to make won of these…. Nice job though.

Mr.Klean

2008-03-09 20:01:57

From: Ron
lol not really because I'm not really interested in RS. I never really was, but before I was more into it. It's still fun to play around with scar though. I may come around every now and then to say hi though. :)

Oh and no it doesn't work with 11 digits. In the script, I'm using LongInts and those are 32-bit and can hold a maximum value of 2,147,483,647. There is another type in scar called Int64, but it doesn't seem to work in loops or anything. I'm guessing Int64 can hold 64-bit information meaning a maximum value of …mm who knows. A lot more though lol.

About the writelns. If I don't put the writelns in, you're right. It's much faster. But I still want to display the information. So if I had the numbers processed first, and then displayed, that comes out to the same time it does if I display the results while processing. I'm guessing you just take the writelns out, but then all you get is how many prime #s found and how long it took.



Maximum value for 64-bit is 9,223,372,036,854,775,807

Yay I think my sig is saved once again.

Harry

2008-03-09 20:15:36

From: mr.klean

Yay I think my sig is saved once again.


99999999977 is an 11 digit prime number :)

MylesMadness

2008-03-09 20:36:03

Okay this is weird, bebe, star, hobbit, solemn, ron.

mixster

2008-03-10 02:58:55

From: mr.klean
Maximum value for 64-bit is 9,223,372,036,854,775,807

Yay I think my sig is saved once again.


Check my topic - it's even got a plugin now for faster 11 digit prime number searching :)

Hobbit

2008-03-18 21:35:46

From: Mylesmadness
Okay this is weird, bebe, star, hobbit, solemn, ron.


I never left…..

Ron

2008-03-18 23:38:01

lol wow a lot of discussion here. Good job everyone. I was just kind of bored but hey if you guys can make it better, go right ahead!

And just for the sake of argument, I never really left. I'm still somewhat active in SRL dev section, but I rarely go on Mopar's forums. I'll try to come and post more random scripts if I find the time. Who knows? maybe one will be as popular as that ban yourself fast script haha. :D

Bobarkinator

2008-03-20 09:19:33

From: Ron
lol wow a lot of discussion here. Good job everyone. I was just kind of bored but hey if you guys can make it better, go right ahead!

And just for the sake of argument, I never really left. I'm still somewhat active in SRL dev section, but I rarely go on Mopar's forums. I'll try to come and post more random scripts if I find the time. Who knows? maybe one will be as popular as that ban yourself fast script haha. :D


I think you've been like that for as long as I've been here :p

kelly slater pro surfer

2008-03-20 16:29:13

yo im gona use this on my homework!!!! =]

Ron

2008-05-24 11:48:38

@Mr. Klean's signature. This was just for fun, not be able to find every prime number. Without a plugin, you can't use only scar to find prime numbers with more than 11 digits and I didn't feel like going through the trouble of making a plugin.

mixster

2008-05-24 14:07:18

Random post from Ron, yay! And you can using Int64, though it runs very slowly and I did make a plugin for it somewhere :)

P1nky

2008-05-27 12:36:04

[offtopic] jw Ron why do i always see you post on Other scripts section :P
lol [offtopic]

Smartzkid

2008-05-27 17:59:19

If we're going down to the ms, use Trunc instead of Floor.

program TvF;
var
l, f, i, j, t: integer;
e: extended;
begin
t := GetSystemTime;
ClearReport;
for j := 1 to 3 do
begin
case j of
1: e := 0.211208926778124;
2: e := 13023908123;
3: e := rand * Random(50000) + rand;
end;

AddToReport(FloatToStr(e));

i:= 1;
f := GetSystemTime;
for i := 1 to 5000 do
Trunc(e);
l := GetSystemTime;
AddToReport('Trunc: ' + inttostr(l - f) + ' ms');

i := 1;
f := GetSystemTime;
for i := 1 to 5000 do
Floor(e);
l := GetSystemTime;
AddToReport('Floor: ' + inttostr(l - f) + ' ms');
end;
writeln('Total test duration: ' + inttostr(GetSystemTime - t) + ' ms');
end.

0.211208926778124
Trunc: 32 ms
Floor: 47 ms
13023908123
Trunc: 15 ms
Floor: 31 ms
16515.5821194125
Trunc: 0 ms
Floor: 32 ms

EDIT: You can also take out lines 40 & 41; you already checked to make sure min and max are bigger than 0, you don't need to do it again.

You also don't need to check of maximum is larger than 0; if min >= 0, and min < max, max must be > 0. (you also need to make Minimum < Maximum, right now it's Minimum <= Maximum)