Taxicab numbers: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) Rename Perl 6 -> Raku, alphabetize, minor clean-up |
|||
Line 918: | Line 918: | ||
2006 : {1677646971, [{891, 990}, {99, 1188}]} |
2006 : {1677646971, [{891, 990}, {99, 1188}]} |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<lang freebasic>' version 11-10-2016 |
<lang freebasic>' version 11-10-2016 |
||
Line 1,499: | Line 1,500: | ||
2006: 1677646971 = 990^3 + 891^3 = 1188^3 + 99^3 |
2006: 1677646971 = 990^3 + 891^3 = 1188^3 + 99^3 |
||
</pre> |
</pre> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<lang JavaScript>var n3s = [], |
<lang JavaScript>var n3s = [], |
||
Line 2,208: | Line 2,210: | ||
2006: 1677646971 = 99^3 + 1188^3 = 891^3 + 990^3 |
2006: 1677646971 = 99^3 + 1188^3 = 891^3 + 990^3 |
||
</pre> |
</pre> |
||
=={{header|Perl 6}}== |
|||
This uses a pretty simple search algorithm that doesn't necessarily return the Taxicab numbers in order. Assuming we want all the Taxicab numbers within some range S to N, we'll search until we find N values. When we find the Nth value, we continue to search up to the cube root of the largest Taxicab number found up to that point. That ensures we will find all of them inside the desired range without needing to search arbitrarily or use magic numbers. Defaults to returning the Taxicab numbers from 1 to 25. Pass in a different start and end value if you want some other range. |
|||
<lang perl6>constant @cu = (^Inf).map: { .³ } |
|||
sub MAIN ($start = 1, $end = 25) { |
|||
my %taxi; |
|||
my int $taxis = 0; |
|||
my $terminate = 0; |
|||
my int $max = 0; |
|||
for 1 .. * -> $c1 { |
|||
last if ?$terminate && ($terminate < $c1); |
|||
for 1 .. $c1 -> $c2 { |
|||
my $this = @cu[$c1] + @cu[$c2]; |
|||
%taxi{$this}.push: [$c2, $c1]; |
|||
if %taxi{$this}.elems == 2 { |
|||
++$taxis; |
|||
$max max= $this; |
|||
} |
|||
$terminate = ceiling $max ** (1/3) if $taxis == $end and !$terminate; |
|||
} |
|||
} |
|||
display( %taxi, $start, $end ); |
|||
} |
|||
sub display (%this_stuff, $start, $end) { |
|||
my $i = $start; |
|||
printf "%4d %10d =>\t%s\n", $i++, $_.key, |
|||
(.value.map({ sprintf "%4d³ + %-s\³", |$_ })).join: ",\t" |
|||
for %this_stuff.grep( { $_.value.elems > 1 } ).sort( +*.key )[$start-1..$end-1]; |
|||
}</lang> |
|||
{{out}}With no passed parameters (default): |
|||
<pre> 1 1729 => 9³ + 10³, 1³ + 12³ |
|||
2 4104 => 9³ + 15³, 2³ + 16³ |
|||
3 13832 => 18³ + 20³, 2³ + 24³ |
|||
4 20683 => 19³ + 24³, 10³ + 27³ |
|||
5 32832 => 18³ + 30³, 4³ + 32³ |
|||
6 39312 => 15³ + 33³, 2³ + 34³ |
|||
7 40033 => 16³ + 33³, 9³ + 34³ |
|||
8 46683 => 27³ + 30³, 3³ + 36³ |
|||
9 64232 => 26³ + 36³, 17³ + 39³ |
|||
10 65728 => 31³ + 33³, 12³ + 40³ |
|||
11 110656 => 36³ + 40³, 4³ + 48³ |
|||
12 110808 => 27³ + 45³, 6³ + 48³ |
|||
13 134379 => 38³ + 43³, 12³ + 51³ |
|||
14 149389 => 29³ + 50³, 8³ + 53³ |
|||
15 165464 => 38³ + 48³, 20³ + 54³ |
|||
16 171288 => 24³ + 54³, 17³ + 55³ |
|||
17 195841 => 22³ + 57³, 9³ + 58³ |
|||
18 216027 => 22³ + 59³, 3³ + 60³ |
|||
19 216125 => 45³ + 50³, 5³ + 60³ |
|||
20 262656 => 36³ + 60³, 8³ + 64³ |
|||
21 314496 => 30³ + 66³, 4³ + 68³ |
|||
22 320264 => 32³ + 66³, 18³ + 68³ |
|||
23 327763 => 51³ + 58³, 30³ + 67³ |
|||
24 373464 => 54³ + 60³, 6³ + 72³ |
|||
25 402597 => 56³ + 61³, 42³ + 69³</pre> |
|||
With passed parameters 2000 2006: |
|||
<pre>2000 1671816384 => 940³ + 944³, 428³ + 1168³ |
|||
2001 1672470592 => 632³ + 1124³, 29³ + 1187³ |
|||
2002 1673170856 => 828³ + 1034³, 458³ + 1164³ |
|||
2003 1675045225 => 744³ + 1081³, 522³ + 1153³ |
|||
2004 1675958167 => 711³ + 1096³, 492³ + 1159³ |
|||
2005 1676926719 => 714³ + 1095³, 63³ + 1188³ |
|||
2006 1677646971 => 891³ + 990³, 99³ + 1188³</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,712: | Line 2,646: | ||
24: 373464 = 6^3 + 72^3 = 54^3 + 60^3 |
24: 373464 = 6^3 + 72^3 = 54^3 + 60^3 |
||
25: 402597 = 42^3 + 69^3 = 56^3 + 61^3</pre> |
25: 402597 = 42^3 + 69^3 = 56^3 + 61^3</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
This uses a pretty simple search algorithm that doesn't necessarily return the Taxicab numbers in order. Assuming we want all the Taxicab numbers within some range S to N, we'll search until we find N values. When we find the Nth value, we continue to search up to the cube root of the largest Taxicab number found up to that point. That ensures we will find all of them inside the desired range without needing to search arbitrarily or use magic numbers. Defaults to returning the Taxicab numbers from 1 to 25. Pass in a different start and end value if you want some other range. |
|||
<lang perl6>constant @cu = (^Inf).map: { .³ } |
|||
sub MAIN ($start = 1, $end = 25) { |
|||
my %taxi; |
|||
my int $taxis = 0; |
|||
my $terminate = 0; |
|||
my int $max = 0; |
|||
for 1 .. * -> $c1 { |
|||
last if ?$terminate && ($terminate < $c1); |
|||
for 1 .. $c1 -> $c2 { |
|||
my $this = @cu[$c1] + @cu[$c2]; |
|||
%taxi{$this}.push: [$c2, $c1]; |
|||
if %taxi{$this}.elems == 2 { |
|||
++$taxis; |
|||
$max max= $this; |
|||
} |
|||
$terminate = ceiling $max ** (1/3) if $taxis == $end and !$terminate; |
|||
} |
|||
} |
|||
display( %taxi, $start, $end ); |
|||
} |
|||
sub display (%this_stuff, $start, $end) { |
|||
my $i = $start; |
|||
printf "%4d %10d =>\t%s\n", $i++, $_.key, |
|||
(.value.map({ sprintf "%4d³ + %-s\³", |$_ })).join: ",\t" |
|||
for %this_stuff.grep( { $_.value.elems > 1 } ).sort( +*.key )[$start-1..$end-1]; |
|||
}</lang> |
|||
{{out}}With no passed parameters (default): |
|||
<pre> 1 1729 => 9³ + 10³, 1³ + 12³ |
|||
2 4104 => 9³ + 15³, 2³ + 16³ |
|||
3 13832 => 18³ + 20³, 2³ + 24³ |
|||
4 20683 => 19³ + 24³, 10³ + 27³ |
|||
5 32832 => 18³ + 30³, 4³ + 32³ |
|||
6 39312 => 15³ + 33³, 2³ + 34³ |
|||
7 40033 => 16³ + 33³, 9³ + 34³ |
|||
8 46683 => 27³ + 30³, 3³ + 36³ |
|||
9 64232 => 26³ + 36³, 17³ + 39³ |
|||
10 65728 => 31³ + 33³, 12³ + 40³ |
|||
11 110656 => 36³ + 40³, 4³ + 48³ |
|||
12 110808 => 27³ + 45³, 6³ + 48³ |
|||
13 134379 => 38³ + 43³, 12³ + 51³ |
|||
14 149389 => 29³ + 50³, 8³ + 53³ |
|||
15 165464 => 38³ + 48³, 20³ + 54³ |
|||
16 171288 => 24³ + 54³, 17³ + 55³ |
|||
17 195841 => 22³ + 57³, 9³ + 58³ |
|||
18 216027 => 22³ + 59³, 3³ + 60³ |
|||
19 216125 => 45³ + 50³, 5³ + 60³ |
|||
20 262656 => 36³ + 60³, 8³ + 64³ |
|||
21 314496 => 30³ + 66³, 4³ + 68³ |
|||
22 320264 => 32³ + 66³, 18³ + 68³ |
|||
23 327763 => 51³ + 58³, 30³ + 67³ |
|||
24 373464 => 54³ + 60³, 6³ + 72³ |
|||
25 402597 => 56³ + 61³, 42³ + 69³</pre> |
|||
With passed parameters 2000 2006: |
|||
<pre>2000 1671816384 => 940³ + 944³, 428³ + 1168³ |
|||
2001 1672470592 => 632³ + 1124³, 29³ + 1187³ |
|||
2002 1673170856 => 828³ + 1034³, 458³ + 1164³ |
|||
2003 1675045225 => 744³ + 1081³, 522³ + 1153³ |
|||
2004 1675958167 => 711³ + 1096³, 492³ + 1159³ |
|||
2005 1676926719 => 714³ + 1095³, 63³ + 1188³ |
|||
2006 1677646971 => 891³ + 990³, 99³ + 1188³</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 3,476: | Line 3,479: | ||
4162 7668767232 = 44^3 +1972^3 = 1384^3 +1712^3 360^3 +1968^3 |
4162 7668767232 = 44^3 +1972^3 = 1384^3 +1712^3 360^3 +1968^3 |
||
4359 8849601000 = 1017^3 +1983^3 = 1530^3 +1740^3 900^3 +2010^3</pre> |
4359 8849601000 = 1017^3 +1983^3 = 1530^3 +1740^3 900^3 +2010^3</pre> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|D}} |
{{trans|D}} |