O'Halloran numbers: Difference between revisions

Add MATLAB/Octave implementation
(Add R implementation)
(Add MATLAB/Octave implementation)
 
(6 intermediate revisions by the same user not shown)
Line 379:
<pre>
Values larger than 6 and less than 1_000 which cannot be the surface area of a cuboid:
8 12 20 36 44 60 84 116 140 156 204 260 380 420 660 924
</pre>
 
=={{header|Dart}}==
{{trans|Scala}}
<syntaxhighlight lang="Dart">
import "dart:io";
 
void main() {
const int maximumArea = 1000;
int halfMaximumArea = maximumArea ~/ 2;
 
List<bool> ohalloranNumbers = List<bool>.filled(halfMaximumArea, true);
 
for (int length = 1; length < maximumArea; length++) {
for (int width = 1; width < halfMaximumArea; width++) {
for (int height = 1; height < halfMaximumArea; height++) {
int halfArea = length * width + length * height + width * height;
if (halfArea < halfMaximumArea) {
ohalloranNumbers[halfArea] = false;
}
}
}
}
 
print("Values larger than 6 and less than 1,000 which cannot be the surface area of a cuboid:");
for (int i = 3; i < halfMaximumArea; i++) {
if (ohalloranNumbers[i]) {
stdout.write('${i * 2} ');
}
}
}
</syntaxhighlight>
{{out}}
<pre>
Values larger than 6 and less than 1,000 which cannot be the surface area of a cuboid:
8 12 20 36 44 60 84 116 140 156 204 260 380 420 660 924
</pre>
Line 463 ⟶ 499:
.
</syntaxhighlight>
 
=={{header|Fortran}}==
{{trans|Julia}}
<syntaxhighlight lang="Fortran">
program ohalloran_numbers
implicit none
integer, parameter :: max_area = 1000
integer :: half_max, i, j, k, area
logical, dimension(max_area) :: areas
half_max = max_area / 2
! Initialize areas with .TRUE.
areas = .TRUE.
do i = 1, max_area
do j = 1, half_max
if (i * j > half_max) exit
do k = 1, half_max
area = 2 * (i * j + i * k + j * k)
if (area > max_area) exit
areas(area) = .FALSE. ! Mark as not an O'Halloran number
end do
end do
end do
! Print the O'Halloran numbers
print *, "Even surface areas < NOT ", max_area, " achievable by any regular integer-valued cuboid:"
do i = 1, max_area
if (mod(i, 2) == 0 .AND. areas(i)) then
write(*,'(I4,1X)', advance='no') i
endif
end do
print *, "" ! To add a final newline after the list
end program ohalloran_numbers
</syntaxhighlight>
{{out}}
<pre>
Even surface areas < NOT 1000 achievable by any regular integer-valued cuboid:
2 4 8 12 20 36 44 60 84 116 140 156 204 260 380 420 660 924
</pre>
 
 
 
Line 684 ⟶ 761:
[2, 4, 8, 12, 20, 36, 44, 60, 84, 116, 140, 156, 204, 260, 380, 420, 660, 924]
</pre>
 
=={{header|Kotlin}}==
{{trans|Scala}}
<syntaxhighlight lang="Kotlin">
object OHalloranNumbers {
 
@JvmStatic
fun main(args: Array<String>) {
val maximumArea = 1_000
val halfMaximumArea = maximumArea / 2
 
var ohalloranNumbers = BooleanArray(halfMaximumArea) { true }
 
for (length in 1 until maximumArea) {
for (width in 1 until halfMaximumArea) {
for (height in 1 until halfMaximumArea) {
val halfArea = length * width + length * height + width * height
if (halfArea < halfMaximumArea) {
ohalloranNumbers[halfArea] = false
}
}
}
}
 
println("Values larger than 6 and less than 1,000 which cannot be the surface area of a cuboid:")
for (i in 3 until halfMaximumArea) {
if (ohalloranNumbers[i]) {
print("${i * 2} ")
}
}
println()
}
}
 
fun main() {
OHalloranNumbers.main(arrayOf())
}
</syntaxhighlight>
{{out}}
<pre>
Values larger than 6 and less than 1,000 which cannot be the surface area of a cuboid:
8 12 20 36 44 60 84 116 140 156 204 260 380 420 660 924
</pre>
 
 
 
=={{header|Lua}}==
Line 761 ⟶ 883:
Even surface areas < 1000 NOT achievable by any regular integer-valued cuboid:
{2, 4, 8, 12, 20, 36, 44, 60, 84, 116, 140, 156, 204, 260, 380, 420, 660, 924}
</pre>
 
=={{header|MATLAB}}/{{header|Octave}}==
{{trans|Julia}}
<syntaxhighlight lang="MATLAB">
max_area = 1000;
half_max = max_area / 2;
areas = ones(1, max_area); % Initialize areas with 1's
 
for i = 1:max_area
for j = 1:half_max
if (i * j > half_max)
break;
endif
for k = 1:half_max
area = 2 * (i * j + i * k + j * k);
if (area > max_area)
break;
endif
areas(area) = 0; % Mark as not an O'Halloran number
endfor
endfor
endfor
 
% Print the O'Halloran numbers
printf("Even surface areas < NOT %d achievable by any regular integer-valued cuboid:\n", max_area);
for n = 1:max_area/2
if (areas(2*n))
printf("%d ", 2*n);
endif
endfor
printf("\n");
</syntaxhighlight>
{{out}}
<pre>
Even surface areas < NOT 1000 achievable by any regular integer-valued cuboid:
2 4 8 12 20 36 44 60 84 116 140 156 204 260 380 420 660 924
 
</pre>
 
Line 907 ⟶ 1,067:
Even surface areas < 1000 NOT achievable by any regular integer-valued cuboid:
8 12 20 36 44 60 84 116 140 156 204 260 380 420 660 924
</pre>
 
=={{header|PHP}}==
{{trans|Scala}}
<syntaxhighlight lang="PHP">
<?php
 
function main() {
$maximumArea = 1000;
$halfMaximumArea = $maximumArea / 2;
 
$ohalloranNumbers = array_fill(0, $halfMaximumArea, true);
 
for ($length = 1; $length < $maximumArea; $length++) {
for ($width = 1; $width < $halfMaximumArea; $width++) {
for ($height = 1; $height < $halfMaximumArea; $height++) {
$halfArea = $length * $width + $length * $height + $width * $height;
if ($halfArea < $halfMaximumArea) {
$ohalloranNumbers[$halfArea] = false;
}
}
}
}
 
echo "Values larger than 6 and less than $maximumArea which cannot be the surface area of a cuboid:\n";
for ($i = 3; $i < $halfMaximumArea; $i++) {
if ($ohalloranNumbers[$i]) {
echo ($i * 2) . " ";
}
}
echo "\n";
}
 
main();
 
?>
</syntaxhighlight>
{{out}}
<pre>
Values larger than 6 and less than 1000 which cannot be the surface area of a cuboid:
8 12 20 36 44 60 84 116 140 156 204 260 380 420 660 924
</pre>
 
Line 989 ⟶ 1,191:
</pre>
 
=={{header|R}}==
{{trans|Julia}}
<syntaxhighlight lang="R">
Line 1,220 ⟶ 1,422:
}
}
</syntaxhighlight>
{{out}}
<pre>
Values larger than 6 and less than 1,000 which cannot be the surface area of a cuboid:
8 12 20 36 44 60 84 116 140 156 204 260 380 420 660 924
</pre>
 
=={{header|Swift}}==
{{trans|Scala}}
<syntaxhighlight lang="Swift">
import Foundation
 
func main() {
let maximumArea = 1_000
let halfMaximumArea = maximumArea / 2
 
var ohalloranNumbers = Array(repeating: true, count: halfMaximumArea)
 
for length in 1..<maximumArea {
for width in 1..<halfMaximumArea {
for height in 1..<halfMaximumArea {
let halfArea = length * width + length * height + width * height
if halfArea < halfMaximumArea {
ohalloranNumbers[halfArea] = false
}
}
}
}
 
print("Values larger than 6 and less than 1,000 which cannot be the surface area of a cuboid:")
for i in 3..<halfMaximumArea where ohalloranNumbers[i] {
print(i * 2, terminator: " ")
}
print()
}
 
// Running the main function
main()
</syntaxhighlight>
{{out}}
337

edits