SRL Archive - SRL Snippets

Fun/Learning Snippets


Le Jingle

2013-01-10 02:09:13

Thought I'd start a thread in the Snippets section for idea's/math related stuffs I've read from various books/articles/web pages/etc. and put them into simba syntax.

If you have a well though up theorem/function, I know there's plentiful choices out there in this world, so feel free to post them!

Collatz Theorem;

(*
if the parameter, st, is odd then
parameter will be multiplied by 3 then increased by 1.

if the parameter, st, is even then
parameter will be halved.

Both if statments validate an increase in the result,
which tracks how many calls to itself it takes for the parameter to reach 1.
*)

function CollatzRecursion(st : integer) : integer;
begin
if (st = 1) then exit;

if ((st mod 2) = 0) then
st := (st div 2)
else
st := ((st * 3) + 1);
writeln(st);
result := CollatzRecursion(st) + 1;
end;
* Idea credit = java docs

result of the above, being used with a parameter of 3;

10
5
16
8
4
2
1
Successfully executed.
This shows what each value of the parameter, st, changes throughout the function.
The result of the function, tells the amount of numbers (as shown in code box) that were used. (in the example above, 7 is the amount/length of the result, when calling parameter, st, with an argument of 3)




{$i srl/srl.simba}

function BunnyMultiply(MaxDays: integer): TIntegerArray;
var
i : integer;
begin
SetLength(result, MaxDays);
for i := 1 to MaxDays do
result[i - 1] := (i - 1) * (i);
DeleteValueInIntArray(result, 0);
end;

var
amount, i : integer;
arr : TIntegerArray;

begin
amount := 10; // 9 days
SetLength(arr, amount);
arr := BunnyMultiply(amount);
for i := 0 to high(arr) do
Writeln('Day ' + IntToStr(i + 1) + ' bunny count = ' + IntToStr(arr[i]));
end.
* credit = java docs - will update better tomorrow

Will add in more as I find them. :)

Cheers,
Lj


Have quite a few Euler project snippets, but I know the point of the site isn't to hand out answers but to learn them.
So, I made a snippet in Lape, that is LIKE a question from their site;

(Sum n prime numbers, given any length of primes to find n (const PRIME_COUNT in code))


{$DEFINE STORE_PRIME_ARRAY}

// Find this many primes and sum them:
const PRIME_COUNT = 10; // 148933; Euler 2M

var count : Integer = 1;
var target : Integer = 2;
var i_ : Integer = 3;
var index : Integer = PRIME_COUNT;
var sum : Int64 = 2;
{$IFDEF STORE_PRIME_ARRAY}
var Primes : array [0..(PRIME_COUNT - 1)] of Int64;
{$ENDIF}

function CheckIfEven(var input: Integer): Boolean;
begin
Result := (input mod 2 = 0);
if (Result) then input := input + 1;
end;

function isPrime(input: Integer): Boolean;
var
i: Integer;
begin
Result := False;
i := 2;
while (i <= Round(Sqrt(input))) do
if (input mod i = 0) then
Exit
else
begin
Inc(i);
CheckIfEven(i);
end;
Result := True;
end;

begin
ClearDebug;
while (count < index) do
begin
if (isPrime(i_)) then
begin
Inc(Count);
target := i_;
{$IFDEF STORE_PRIME_ARRAY}
Primes[count - 1] := target;
{$ENDIF}
sum := sum + target;
end;
i_ := i_ + 2;
end;
{$IFDEF STORE_PRIME_ARRAY}
Primes[0] := 2;
Writeln(Primes);
{$ENDIF}
Writeln(Format('Last Prime (%d) @ index = %d', [target, index]));
end.

Le Jingle

2013-02-18 21:56:01

Factorials
-old removed!

new Factorials:

function fac(n : integer) : integer;
var
i : integer;
begin
result := 1;
for i := 1 to n do
result := result * i;
end;

i luffs yeww

2013-02-18 22:02:25

From: Le Jingle
Factorials

var
s : string;

function Factorial(n : integer): integer;
begin
if (n = 0) then exit;
s := s + '*' + IntToStr(n); // Cba #fence post
result := Factorial(n - 1) * n;
end;

begin
factorial(10);
writeln(s);
end.


This doesn't actually calculate the factorial of n, by the way. The result is always 0.

Runehack123

2013-02-18 22:04:18

I love this! ;)
Some more debug examples for your last two would be nice. :)

Good job, good thread.

Le Jingle

2013-02-18 22:04:44

From: i luffs yeww
This doesn't actually calculate the factorial of n, by the way. The result is always 0.


opps good catch, base case should be == 1 :]
Cheers

edit lol, I got caught up string testing it, forgot that result should be the actual factorial of the called parameter n
>.<

CynicRus

2013-02-19 01:02:03

From: Le Jingle
Factorials
-old removed!

new Factorials:

function fac(n : integer) : integer;
var
i : integer;
begin
result := 1;
for i := 1 to n do
result := result * i;
end;

Also you can calculate it as such:

Function Factorial(Var A: Integer): Extended;
Var
X: Integer;
Begin
Result := 1;
For X := A Downto 1 Do
Result := Result * X;
End;

Le Jingle

2013-04-22 01:02:22


function fac(n: Int64): Int64;
var
i: Int64;
begin
Result := 1;
i := 1;
while (i <= n) do
begin
Result := Result * i;
i := i + 1;
end;
end;


function IsPrime(input: Int64): Boolean;
var
index: Int64;
begin
Result := False;
if (input < 2) then
Exit;
index := 2;
while (index <= Sqrt(input)) do
begin
if (input mod index = 0) then
Exit;
index := index + 1;
end;
Result := True;
end;

Le Jingle

2013-09-27 17:37:56

don't think I have these 2 added yet:


function GCD(a, b: Integer): Integer;
var c: Integer;
begin
while (a <> 0) do
begin
c := a;
a := b mod a;
b := c;
end;
Result := b;
end;


function isNotZero(n: Integer): Boolean;
begin
Result := ((n or (n + 2147483647)) shr 31) and 1;
end;