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)
SCAR Code: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:
And a C# Version :)Code: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");
}
}
}
Code: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):
Code: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

