SRL Archive - Other Scripts

Ramanujan Cubes


Nadeem

2009-09-05 03:10:53

Hi,

I made a simple Ramanujan cube solver (my teacher asked me to make a quick one to demonstrate to his grade 12 class on the first day of school). I made the original one in Java, but made a scar version aswell for fun :) Has the ability to do about 19.6 million numbers per second and Taxicab(3) within 7.5 seconds using JAVA, but SCAR can only do roughly 50,000 numbers per second, might take a few minutes when trying Taxicab(3) on SCAR. (these result are for me, on 2x1.83ghz processor)


procedure Ramanujan(rnum: Integer);
var i, j, k, n: Integer;
i3, j3, k3, n3: Integer;
matches, started: Integer;
begin
started := GetSystemTime;

repeat
i := i + 1;
i3 := i * i * i;
j := i;
if (i3 > rnum) then break;
repeat
j := j + 1;
j3 := j * j * j;
k := i + 1;
if (i3 + j3 > rnum) then break;
repeat
k := k + 1;
k3 := k * k * k;
n := k;
if (k3 > i3 + j3) then break;
repeat
n := n + 1;
n3 := n * n * n;
if (n3 + k3 > i3 + j3) then break;
if (n3 + k3 = i3 + j3) then
begin
//Writeln(Format('%d = %d^3 + %d^3 = %d^3 + %d^3', [i3 + j3, i, j, k, n]));
matches := matches + 1;
end;
until(n >= rnum);
until(k >= rnum);
until(j >= rnum);
until (i >= rnum);

Writeln(Format('Found %d Matches | Time Took: %dms', [matches, GetSystemTime - started]));
end;

begin
Ramanujan(StrToInt(Readln('Number')));
end.

—

Java Version:

class Ramanujan
{
Ramanujan(int rnum)
{
long time = System.currentTimeMillis();

int matches = 0;
int i = 0, j = 0, k = 0, n = 0;
int i3, j3, k3, n3;

do
{
i++;
i3 = i * i * i;
j = i;
do
{
j++;
j3 = j * j * j;
k = i + 1;
do
{
k++;
k3 = k * k * k;
n = k;
do
{
n++;
n3 = n * n * n;
if (n3 + k3 == i3 + j3)
{
//System.out.println((i3 + j3) + " = " + i + "^3 + " + j + "^3 = " + k + "^3 + " + n + "^3");
matches++;
}
} while (n <= rnum && n3 + k3 < i3 + j3);
} while (k <= rnum && k3 < i3 + j3);
} while (j <= rnum && i3 + j3 < rnum);
} while (i <= rnum && i3 < rnum);

System.out.println("Found " + matches + " Matches | Time Took: " + (double)(System.currentTimeMillis() - time) / 1000);
}

static void main(String[] args)
{
try
{
int num = Integer.parseInt(new java.util.Scanner(System.in).next());
new Ramanujan(num);
}
catch(NumberFormatException nfe)
{
System.out.println("Please enter a numerical digit");
}
}
}

And a C# Version :)

using System;
using System.Collections.Generic;

namespace Ramanujan
{
class Ramanujan
{
Ramanujan(int rnum)
{
List<string> list = new List<string>();
int matches = 0;
int i = 0, j = 0, k = 0, n = 0;
int i3, j3, k3, n3;
int timestart = Environment.TickCount;

do
{
i++;
i3 = i * i * i;
j = i;
do
{
j++;
j3 = j * j * j;
k = i + 1;
do
{
k++;
k3 = k * k * k;
n = k;
do
{
n++;
n3 = n * n * n;
if (n3 + k3 == i3 + j3)
{
list.Add((i3 + j3) + " = " + i + "^3 + " + j + "^3 = " + k + "^3 + " + n + "^3");
matches++;
}
} while (n <= rnum && n3 + k3 < i3 + j3);
} while (k <= rnum && k3 < i3 + j3);
} while (j <= rnum && i3 + j3 < rnum);
} while (i <= rnum && i3 < rnum);

double timetook = (Environment.TickCount - timestart) / 1000;
list.ForEach(new Action<string>(Console.WriteLine));
Console.WriteLine("Found {0} Matches | Time Took: {1}s", matches, timetook);
}

static void Main(string[] args)
{
Console.Write("Enter Search Limit: ");
try
{
int limit = Int32.Parse(Console.ReadLine().Trim());
new Ramanujan(limit);
}
catch (FormatException fe) { throw new FormatException(fe.Message); }
Console.Read();
}
}
}


LOLCODE version (as suggested by Freddy):

HAI
CAN HAS STDIO?
I HAS A rnum
I HAS A matches

BTW fill in the number limit on next line
rnum IZ 100000

VISIBLE "Starting Ramanujan, Limit: " rnum
IM IN YR LOOP, UPPIN YR i!!1 TILL rnum
I HAS A i3 ITZ i BOOM 3
IS i3 BIGGER THAN rnum?
YA RLY
THATS ALL
OKTHX

I HAS A j ITZ i TIEMZ 1
IM IN YR INNER_LOOP, UPPIN YR j!!1 TILL rnum
I HAS A j3 ITZ j BOOM 3
IS i3 UP j3 BIGGER THAN rnum?
YA RLY
THATS ALL
OKTHX

I HAS A k ITZ i UP 1
IM IN YR LOOP, UPPIN YR k!!1 TILL rnum
I HAS A k3 ITZ k BOOM 3
IS k3 BIGGER THAN i3 UP j3?
YA RLY
THATS ALL
OKTHX

I HAS A n ITZ k
IM IN YR INNER_LOOP, UPPIN YR n!!1 TILL rnum
I HAS A n3 ITZ n BOOM 3

IS n3 UP k3 BIGGER THAN i3 UP j3?
YA RLY
THATS ALL
OKTHX

IS n3 UP k3 LIEK i3 UP j3?
YA RLY
VISIBLE i3 UP j3 " = " i "^3 + " j "^3 = " k "^3 + " n "^3"
UP matches!!1
OKTHX
IM OUTTA YR INNER_LOOP
IM OUTTA YR LOOP
IM OUTTA YR INNER_LOOP
IM OUTTA YR LOOP
VISIBLE "Found a total of: " matches " matches!"
KTHXBAI



~NS

caused

2009-09-06 14:14:34

Whats a Ramanujan cube :D ?

Nadeem

2009-09-06 20:16:05

From: caused
Whats a Ramanujan cube :D ?


They are numbers cubed which happen to have summed matches, well kinda hard for my to explain without showing you anything so: http://www.durangobill.com/Ramanujan.html



~NS

marpis

2009-09-06 22:56:13

That Ramanujan was insane… He has invented insane Pi approximations without any calculators :S For example

I like solving maths problems with computer :)

Nadeem

2009-09-06 23:12:36

From: marpis
That Ramanujan was insane… He has invented insane Pi approximations without any calculators :S For example

I like solving maths problems with computer :)


lol yea he was a mathematical genius :p friggin mastered trig by age 13 and lead a whole damn research by age 17…crazy.



~NS

Sandstorm

2009-09-07 01:51:21

http://www.wolframalpha.com/input/?i=%2899^2%29+%2F+%282206+*+1.4142135623730950488016887242097%29


Dayum. I had to use a computer to do it, and he did it on his own?

~Sandstorm

marpis

2009-09-07 16:57:15

From: Sandstorm
http://www.wolframalpha.com/input/?i=%2899^2%29+%2F+%282206+*+1.4142135623730950488016887242097%29


Dayum. I had to use a computer to do it, and he did it on his own?

~Sandstorm


you mean
http://www.wolframalpha.com/input/?i=(99^2)+%2F+(2206+*+1.4142135623730950488016887242097)
^^

Here's even more accurate
http://www.wolframalpha.com/input/?i=(99^2)+%2F+(2206+*+sqrt(2))
7 decimals correct!

Freddy1990

2009-09-13 12:23:35

Pff, no lolcode version…

Laur€ns

2009-09-13 17:48:16

From: Freddy1990
Pff, no lolcode version…


Do want!

Nadeem

2009-09-13 21:06:19

lol, Fine made a lolcode version :p Check first post!



~NS

Freddy1990

2009-09-13 21:11:45

From: Nadeem
lol, Fine made a lolcode version :p Check first post!



~NS


*hug*

ABC.def.GHI

2009-09-13 21:12:15

So.. Much.. Win.. Haha ^^ Pretty cool stuff.. I guess we were doing these in my math class a few days ago, but they didn't say what the numbers were called.. :p

Nadeem

2009-09-13 21:20:09

From: Freddy1990
*hug*

tehe :p
From: ABC.def.GHI
So.. Much.. Win.. Haha ^^ Pretty cool stuff.. I guess we were doing these in my math class a few days ago, but they didn't say what the numbers were called.. :p

probably something similar, but a bit different :) all my math teachers know what's Ramanujan (done it in uni) so assuming most math teachers would know it when they are doing it :)



~NS

ABC.def.GHI

2009-09-13 21:33:15

From: Nadeem
tehe :p

probably something similar, but a bit different :) all my math teachers know what's Ramanujan (done it in uni) so assuming most math teachers would know it when they are doing it :)



~NS


It was actually a lower math class (my math somehow didn't get on my transcript.. I got changed to a different math class now :p) and I didn't go to class the day after we did those numbers.. Oh wait I just looked at the formula again and we were doing something else.. >_> Haha STILL! It's interesting. :)

g0tp0t

2010-03-26 23:21:08

From: marpis
That Ramanujan was insane… He has invented insane Pi approximations without any calculators :S For example

I like solving maths problems with computer :)


wrong

i had scar calculate that, so whats the real calculation of pi

i have always wondered how that is known

[PHP]program New;
var pi:double;
begin
pi := (66*sqrt(2))/((33*sqrt(29))-148)
writeln(pi);
end.[/PHP]


\/supposed to be 2654 but otherwise pretty d4mn accurate
3.14159263220337 is the result

Brain

2010-03-26 23:26:01

From: g0tp0t
wrong

i had scar calculate that, so whats the real calculation of pi



That's why he said it was an approximation…

also, please check post dates before you post, unless of course it is something so related to the thread that you don't want to justify creating a new one.