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;
result of the above, being used with a parameter of 3;
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)
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))
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 docsresult 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 tomorrowWill 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.