Tupper's self-referential formula: Difference between revisions

m
(New post.)
 
(10 intermediate revisions by 6 users not shown)
Line 17:
Make a drawing of the Tupper's formula, either using text, a matrix or creating an image.
 
This task requires arbitrary precision integer operations. If your language does not intrinsecallyintrinsically support that, you can use a library.
 
;Epilogue
Line 33:
Draws the bitmap using ASCII. As only integer arithmetic is used, there's no need for explicit floor operations, and the values of the right-hand side of the inequality can only be 0 or 1.
<syntaxhighlight lang="algol68">
BBEGINBEGIN CO plot Tupper's self-referential formula #
need to find x, y such that:
1/2 < floor( mod( (y/17)*2^ - ( 17x - mod(y,17) ), 2 ) )
Line 59:
print( ( " " ) )
ELSE
LONG LONG INT v := IF power of 2 > 0 THEN 0 ELSE( k + y delta ) OVER 17 FI;
IF power of 2 < 0 THEN
v OVERAB LONG LONG 2 ^ ABS power of 2
FI;
print( ( IF ODD v MODABTHEN "#" ELSE " " FI ) 2;)
print( ( IF v = 0 THEN " " ELSE "#" FI ) )
FI
OD;
Line 90 ⟶ 89:
# # # # # #
### # ### ### # ###
</pre>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Numerics;
 
class TuppersSelfReferentialFormula
{
private static readonly BigInteger k = BigInteger.Parse("960939379918958884971672962127852754715" +
"004339660129306651505519271702802395266424689642842174350718121267153782" +
"770623355993237280874144307891325963941337723487857735749823926629715517" +
"173716995165232890538221612403238855866184013235585136048828693337902491" +
"454229288667081096184496091705183454067827731551705405381627380967602565" +
"625016981482083418783163849115590225610003652351370343874461848378737238" +
"198224849863465033159410054974700593138339226497249461751545728366702369" +
"745461014655997933798537483143786841806593422227898388722980000748404719");
 
static void Main(string[] args)
{
bool[,] matrix = TuppersMatrix();
 
Console.BackgroundColor = ConsoleColor.Magenta;
for (int row = 0; row < 17; row++)
{
for (int column = 0; column < 106; column++)
{
Console.Write(matrix[row, column] ? "█" : " ");
}
Console.WriteLine();
}
Console.ResetColor(); // Resets to the default console background and foreground colors
}
 
private static bool[,] TuppersMatrix()
{
bool[,] matrix = new bool[17, 106];
BigInteger seventeen = new BigInteger(17);
 
for (int column = 0; column < 106; column++)
{
for (int row = 0; row < 17; row++)
{
BigInteger y = k + row;
BigInteger a = y / seventeen;
int bb = (int)(y % seventeen) + column * 17;
BigInteger b = BigInteger.Pow(2, bb);
a /= b;
int aa = (int)(a % 2);
matrix[row, 105 - column] = (aa == 1);
}
}
return matrix;
}
}
</syntaxhighlight>
{{out}}
<pre>
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
██ █ █ █ █ ██ █ █ █ █ █ █ ██ ████ ███ ███ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ ███ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ ██ █ █
███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ ██ █ ██ ███ █ █ █ █ ███ ███ █ ███ ███ █ █ █ █ █
███ █ █ █ █ █ █ █ █ █ █ █ ████ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
██ █ █ █ █ █ ██ ███ █ █ █ ██ █ ████ ████ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
███ █ █ █ █ █ █ █ █
█ █ █ █ █ █
███ █ ███ ███ █ ███
 
</pre>
 
=={{header|C++}}==
{{libheader|Boost}}
<syntaxhighlight lang="cpp">#include <iostream>
 
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/integer.hpp>
 
int main() {
using boost::multiprecision::cpp_int;
using boost::multiprecision::pow;
 
const cpp_int k(
"9609393799189588849716729621278527547150043396601293066515055192717028"
"0239526642468964284217435071812126715378277062335599323728087414430789"
"1325963941337723487857735749823926629715517173716995165232890538221612"
"4032388558661840132355851360488286933379024914542292886670810961844960"
"9170518345406782773155170540538162738096760256562501698148208341878316"
"3849115590225610003652351370343874461848378737238198224849863465033159"
"4100549747005931383392264972494617515457283667023697454610146559979337"
"98537483143786841806593422227898388722980000748404719");
const int rows = 17;
const int columns = 106;
const cpp_int two(2);
 
for (int row = 0; row < rows; ++row) {
for (int column = columns - 1; column >= 0; --column) {
cpp_int y = k + row;
int b = static_cast<int>(y % 17) + column * 17;
cpp_int a = (y / 17) / pow(two, b);
std::cout << ((a % 2 == 1) ? u8"\u2588" : u8" ");
}
std::cout << '\n';
}
}</syntaxhighlight>
 
{{out}}
<pre style="font-size: .75em;">
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
██ █ █ █ █ ██ █ █ █ █ █ █ ██ ████ ███ ███ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ ███ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ ██ █ █
███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ ██ █ ██ ███ █ █ █ █ ███ ███ █ ███ ███ █ █ █ █ █
███ █ █ █ █ █ █ █ █ █ █ █ ████ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
██ █ █ █ █ █ ██ ███ █ █ █ ██ █ ████ ████ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
███ █ █ █ █ █ █ █ █
█ █ █ █ █ █
███ █ ███ ███ █ ███
</pre>
 
Line 100 ⟶ 233:
First, let us make the definition of k:
 
[[File:Fōrmulæ - Tupper's self-referential formula 01a01.png]]
 
Tupper uses the notation '''mod(a, b)''' for the modulo operation, while Fōrmulæ uses the notation '''a Mod b'''. If we want to use the Tupper's formula exactly as it is, we can use the trick:
Line 108 ⟶ 241:
Now we can use a formula that looks exactly as Tupper's:
 
[[File:Fōrmulæ - Tupper's self-referential formula 03a03.png]]
 
'''Visualization as a matrix.''' The following creates a 17×106 matrix. Values for which inequality is true are shown as an opaque black color, otherwise they are shown as an transparent color.
Line 115 ⟶ 248:
 
(image is resized 50% of its original)
 
[[File:Fōrmulæ - Tupper's self-referential formula 05.png|1070px]]
 
'''Visualization as an image.''' In the following script, a function to draw the Tupper's formula is used to generate the image with different sizes, because the standard 1x1 pixel per unit results to be too small in order to be appreciated:
 
[[File:Fōrmulæ - Tupper's self-referential formula 06a06.png]]
 
[[File:Fōrmulæ - Tupper's self-referential formula 0807.png]]
 
[[File:Fōrmulæ - Tupper's self-referential formula 07a08.png]]
 
=={{header|J}}==
Line 195 ⟶ 329:
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Arrays;
 
import javax.imageio.ImageIO;
 
public final class TuppersSelfReferentialFormula {
Line 210 ⟶ 339:
boolean[][] matrix = tuppersMatrix();
PrintStream writer = new PrintStream(System.out, true, "UTF-8");
BufferedImage image = new BufferedImage(1060, 170, BufferedImage.TYPE_INT_RGB);
writer.println("\033[45m"); // Magenta background
Graphics g = image.createGraphics();
for ( int columnrow = 0; columnrow < 10617; columnrow++ ) {
for ( int rowcolumn = 0; rowcolumn < 17106; rowcolumn++ ) {
String character = g.setColor(matrix[row][column] ? Color.BLACK"\u2588" : Color.WHITE)" ";
writer.print(character);
g.fillRect(column * 10, row * 10, 10, 10);
} }
writer.println();
}
}
writer.close();
ImageIO.write(image, "png", new File("TuppersJava.png"));
}
Line 254 ⟶ 383:
</syntaxhighlight>
{{ out }}
[[Media:TuppersJava.PNGpng]]
 
=={{header|Julia}}==
Line 338 ⟶ 467:
{{out}}
Same as Algol 68.
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{trans|Julia}}
<syntaxhighlight lang="Mathematica">
(*Define the Tupper's self-referential formula function*)
tupperMat[k_] :=
Table[1 -
Mod[Floor[Mod[Floor[y/17] 2^(-17 Floor[x] - Mod[y, 17]), 2]],
2], {y, k, k + 16}, {x, 0, 105}]
 
(*Define the constant k*)
k = 960939379918958884971672962127852754715004339660129306651505519271\
7028023952664246896428421743507181212671537827706233559932372808741443\
0789132596394133772348785773574982392662971551717371699516523289053822\
1612403238855866184013235585136048828693337902491454229288667081096184\
4960917051834540678277315517054053816273809676025656250169814820834187\
8316384911559022561000365235137034387446184837873723819822484986346503\
3159410054974700593138339226497249461751545728366702369745461014655997\
933798537483143786841806593422227898388722980000748404719;
 
(*Display the heatmap*)
ArrayPlot[Map[Reverse, tupperMat[k]], AspectRatio -> 1/6,
Frame -> False, ImageSize -> Large,
ColorRules -> {0 -> Black, 1 -> White}]
</syntaxhighlight>
{{out}}
[[File:Tupper's self-referential formula.png|thumb]]
 
 
 
=={{header|Nim}}==
Line 378 ⟶ 536:
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
You can run this online [http://phix.x10.mx/p2js/tupper.htm here]. The pixel/canvas/window sizes are a little off compared to desktop/Phix both initially and when resizing, the latter also goes quite a bit smaller (see below), hopefully all that'll be fixed in js when the new gui that I'm working on right now finally gets shipped.
<!--<syntaxhighlight lang="phix">(phixonline)-->
Line 576 ⟶ 735:
 
The culprit here is BigRat which is written entirely in Wren and always uses maximum precision. Unfortunately, we can't use GMP in a DOME application which would be much faster than this.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color, Font
import "./plot" for Axes
2,120

edits