Tupper's self-referential formula: Difference between revisions
→{{header|ALGOL 68}}: Teeaks and fix typo |
added Raku programming solution |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 89: | Line 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> |
</pre> |
||
Line 155: | Line 233: | ||
First, let us make the definition of k: |
First, let us make the definition of k: |
||
[[File:Fōrmulæ - Tupper's self-referential formula |
[[File:Fōrmulæ - Tupper's self-referential formula 01.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: |
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 163: | Line 241: | ||
Now we can use a formula that looks exactly as Tupper's: |
Now we can use a formula that looks exactly as Tupper's: |
||
[[File:Fōrmulæ - Tupper's self-referential formula |
[[File:Fōrmulæ - Tupper's self-referential formula 03.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. |
'''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 170: | Line 248: | ||
(image is resized 50% of its original) |
(image is resized 50% of its original) |
||
[[File:Fōrmulæ - Tupper's self-referential formula 05.png|1070px]] |
[[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: |
'''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 |
[[File:Fōrmulæ - Tupper's self-referential formula 06.png]] |
||
[[File:Fōrmulæ - Tupper's self-referential formula |
[[File:Fōrmulæ - Tupper's self-referential formula 07.png]] |
||
[[File:Fōrmulæ - Tupper's self-referential formula |
[[File:Fōrmulæ - Tupper's self-referential formula 08.png]] |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 388: | Line 467: | ||
{{out}} |
{{out}} |
||
Same as Algol 68. |
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}}== |
=={{header|Nim}}== |
||
Line 555: | Line 663: | ||
███ █ ███ ███ █ ███ |
███ █ ███ ███ █ ███ |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
<syntaxhighlight lang="raku" line># 20240802 Raku programming solution |
|||
my ( \H, \W, \N ) = 17, 106, 4858450636189713423582095962494202044581400587983244549483093085061934704708809928450644769865524364849997247024915119110411605739177407856919754326571855442057210445735883681829823754139634338225199452191651284348332905131193199953502413758765239264874613394906870130562295813219481113685339535565290850023875092856892694555974281546386510730049106723058933586052544096664351265349363643957125565695936815184334857605266940161251266951421550539554519153785457525756590740540157929001765967965480064427829131488548259914721248506352686630476300; |
|||
for N+H-1 ... N -> \y { |
|||
for ^W -> \x { |
|||
print 0.5 < (y div H) div 2**(H * x + y % H) % 2 ?? '█' !! ' ' |
|||
} |
|||
say(); |
|||
}</syntaxhighlight> |
|||
You may [https://ato.pxeger.com/run?1=NVK7bhRBEJQI7yvagXV3fpz6Pd0y4PSiS52ckCwhSxbiIWMjn5BzvoCExAF8CL9ByJdQe8ar3dndma6qrpr58fPm8t3d4-Ovu9ur0_rz4vf7HS1ouz6h7QWeDS3pFck4IeE8Ia8oD05LqR5irhal3NGp3q6s7B4lzhw1ukzxi4UybuMCUtp8MO4q7tY9m_vIroxQt_Ty7h6KElBKiLQIu0hyDGsZw3lUZEuPcNOMIRXhEI-hMukPNFWWJaVdaigT6zQ3K9WQbg8Fa4ZA39CcaXOIQcqw2mEBbQGwRoZaK7oanmLW3pw1WIwjVRteDVxeIgLFQAXQAVRPdlmtRjB8RhZooBzRw7UkPA2ehYcxwyjnUJBWG7qHV4Ul7sx0Q58Z5m3I3SEwRCeJROyTyxC4gI0YEywhwpIokek7xFUiOIALh3kJQ3wICQoDLM0INACJ0WiaBZY7R2d48bQ3OhAjwvECrDS6xRG0-rSdBsHKNOwoBj6bza4-3tDmeH0qtFqtaEOnr2m7o68zIpqW3lzsZ-6fZnB9urn-cEu8CnpJix29vf5C6-X-pUdHizUd0T0d044Op-lDUjo_p_nf79_mdHBAc5pPNA_T8Plyt1iezR6eTvL_A_18sP8B Attempt This Online!] |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 627: | Line 749: | ||
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. |
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=" |
<syntaxhighlight lang="wren">import "dome" for Window |
||
import "graphics" for Canvas, Color, Font |
import "graphics" for Canvas, Color, Font |
||
import "./plot" for Axes |
import "./plot" for Axes |
Latest revision as of 16:17, 2 August 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- Introduction
Jeff Tupper, in his 2001 paper "Reliable Two-Dimensional Graphing Methods for Mathematical Formulae with Two Free Variables", shows a set of methods to graph equations and inequalities with two variables in the cartesian plane.
One of the examples of the paper, refers to the inequality:
That inequality, once plotted in the range 0 ≤ x ≤ 106 and k ≤ y ≤ k + 17 for k = 960, 939, 379, 918, 958, 884, 971, 672, 962, 127, 852, 754, 715, 004, 339, 660, 129, 306, 651, 505, 519, 271, 702, 802, 395, 266, 424, 689, 642, 842, 174, 350, 718, 121, 267, 153, 782, 770, 623, 355, 993, 237, 280, 874, 144, 307, 891, 325, 963, 941, 337, 723, 487, 857, 735, 749, 823, 926, 629, 715, 517, 173, 716, 995, 165, 232, 890, 538, 221, 612, 403, 238, 855, 866, 184, 013, 235, 585, 136, 048, 828, 693, 337, 902, 491, 454, 229, 288, 667, 081, 096, 184, 496, 091, 705, 183, 454, 067, 827, 731, 551, 705, 405, 381, 627, 380, 967, 602, 565, 625, 016, 981, 482, 083, 418, 783, 163, 849, 115, 590, 225, 610, 003, 652, 351, 370, 343, 874, 461, 848, 378, 737, 238, 198, 224, 849, 863, 465, 033, 159, 410, 054, 974, 700, 593, 138, 339, 226, 497, 249, 461, 751, 545, 728, 366, 702, 369, 745, 461, 014, 655, 997, 933, 798, 537, 483, 143, 786, 841, 806, 593, 422, 227, 898, 388, 722, 980, 000, 748, 404, 719 produces a drawing that visually mimics the inequality itself, hence it is called self-referential.
Although the inequality is intended to be drawn on the continuum of the cartesian plane, the drawing can be performed iterating over the integer values of both the horizontal and vertical ranges.
- Task
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 intrinsically support that, you can use a library.
- Epilogue
The value of k is an encoding of the bitmap of the image, therefore any 17-width bitmap can be produced, using its associated encoded value as k.
- References
ALGOL 68
Uses Algol 68G's LONG LONG INT which has programmer specifiable precision.
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.
BEGIN 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 ) )
where x in 0..106, y in k..k+16
CO
PR precision 600 PR
LONG LONG INT k = 960 939 379 918 958 884 971 672 962 127 852 754 715 004 339 660 129 306 651 505
519 271 702 802 395 266 424 689 642 842 174 350 718 121 267 153 782 770 623 355
993 237 280 874 144 307 891 325 963 941 337 723 487 857 735 749 823 926 629 715
517 173 716 995 165 232 890 538 221 612 403 238 855 866 184 013 235 585 136 048
828 693 337 902 491 454 229 288 667 081 096 184 496 091 705 183 454 067 827 731
551 705 405 381 627 380 967 602 565 625 016 981 482 083 418 783 163 849 115 590
225 610 003 652 351 370 343 874 461 848 378 737 238 198 224 849 863 465 033 159
410 054 974 700 593 138 339 226 497 249 461 751 545 728 366 702 369 745 461 014
655 997 933 798 537 483 143 786 841 806 593 422 227 898 388 722 980 000 748 404
719;
INT k mod 17 = SHORTEN SHORTEN ( k MOD 17 );
FOR y delta FROM 0 TO 16 DO
FOR x FROM 106 BY -1 TO 0 DO
INT power of 2 = - ( 17 * x ) - ( k mod 17 + y delta ) MOD 17;
# if power of 2 = 0, then ( v * 2 ^ power of 2 ) MOD 2 = v MOD 2 #
# if power of 2 > 0, then ( v * 2 ^ power of 2 ) MOD 2 = 0 #
IF power of 2 > 0 THEN
print( ( " " ) )
ELSE
LONG LONG INT v := ( k + y delta ) OVER 17;
IF power of 2 < 0 THEN
v OVERAB LONG LONG 2 ^ ABS power of 2
FI;
print( ( IF ODD v THEN "#" ELSE " " FI ) )
FI
OD;
print( ( newline ) )
OD
END
- Output:
# # # ## # # # # # # # ## # # # # # # # # # # # # # # # # # # # # ## # # # # ## # # # # # # ## #### ### ### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ### ### # # # # # # # # # # # # # # # # ## # # # # # # # # ## # # ### # # # # # # # # # # # # # # # # # # # # # # ## # ## ### # # # # ### ### # ### ### # # # # # ### # # # # # # # # # # # #### # # # # # # # # # # # # # # # # # # # # # ## # # # # # ## ### # # # ## # #### #### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ### # # # # # # # # # # # # # # ### # ### ### # ###
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;
}
}
- Output:
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ ██ █ █ █ █ █ █ ██ ████ ███ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ ██ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ ██ ███ █ █ █ █ ███ ███ █ ███ ███ █ █ █ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ ████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ █ ██ ███ █ █ █ ██ █ ████ ████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ █ ███ ███ █ ███
C++
#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';
}
}
- Output:
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ ██ █ █ █ █ █ █ ██ ████ ███ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ ██ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ ██ ███ █ █ █ █ ███ ███ █ ███ ███ █ █ █ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ ████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ █ ██ ███ █ █ █ ██ █ ████ ████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ █ ███ ███ █ ███
Fōrmulæ
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.
Programs in Fōrmulæ are created/edited online in its website.
In this page you can see and run the program(s) related to this task and their results. You can also change either the programs or the parameters they are called with, for experimentation, but remember that these programs were created with the main purpose of showing a clear solution of the task, and they generally lack any kind of validation.
Solution
First, let us make the definition of k:
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:
Now we can use a formula that looks exactly as Tupper's:
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.
(image is resized 50% of its original)
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:
J
Tacit Solution
K=. (". (0 ( : 0)) -. LF)"_
960939379918958884971672962127852754715004339660129306651505
519271702802395266424689642842174350718121267153782770623355
993237280874144307891325963941337723487857735749823926629715
517173716995165232890538221612403238855866184013235585136048
828693337902491454229288667081096184496091705183454067827731
551705405381627380967602565625016981482083418783163849115590
225610003652351370343874461848378737238198224849863465033159
410054974700593138339226497249461751545728366702369745461014
655997933798537483143786841806593422227898388722980000748404
719x
)
inequality=. 1r2 < <. o (2 | (17 %~ ]) * 2 ^ (_17 * <. o [) - 17x | <.(o=. @:)])
Tupperimage=. ({&' *') o ([ (] inequality K + [)"0/ |. o ])&:i. f.
Example use:
17 Tupperimage 106
Produces the following text-based image,
- Output:
* * * ** * * * * * * * ** * * * * * * * * * * * * * * * * * * * * ** * * * * ** * * * * * * ** **** *** *** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *** *** * * * * * * * * * * * * * * * * ** * * * * * * * * ** * * *** * * * * * * * * * * * * * * * * * * * * * * ** * ** *** * * * * *** *** * *** *** * * * * * *** * * * * * * * * * * * **** * * * * * * * * * * * * * * * * * * * * * ** * * * * * ** *** * * * ** * **** **** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *** * * * * * * * * * * * * * * *** * *** *** * ***
The code is tacit and fixed (in other words, it is point-free):
_80 [\ (5!:5) <'Tupperimage'
{&' *'@:([ (] (1r2 < <.@:(2 | (17 %~ ]) * 2 ^ (_17 * <.@:[) - 17x | <.@:])) 9609
39379918958884971672962127852754715004339660129306651505519271702802395266424689
64284217435071812126715378277062335599323728087414430789132596394133772348785773
57498239266297155171737169951652328905382216124032388558661840132355851360488286
93337902491454229288667081096184496091705183454067827731551705405381627380967602
56562501698148208341878316384911559022561000365235137034387446184837873723819822
48498634650331594100549747005931383392264972494617515457283667023697454610146559
97933798537483143786841806593422227898388722980000748404719x"_ + [)"0/ |.@:])&:i
.
Alternatively,
Tuppergraph=. viewmat o ([ load o ('viewmat'"_)) o ([ (] inequality K + [)"0/ |. o ])&:i.
17 Tuppergraph 106
produces the following graph,
Java
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Arrays;
public final class TuppersSelfReferentialFormula {
public static void main(String[] args) throws IOException {
boolean[][] matrix = tuppersMatrix();
PrintStream writer = new PrintStream(System.out, true, "UTF-8");
writer.println("\033[45m"); // Magenta background
for ( int row = 0; row < 17; row++ ) {
for ( int column = 0; column < 106; column++ ) {
String character = matrix[row][column] ? "\u2588" : " ";
writer.print(character);
}
writer.println();
}
writer.close();
}
private static boolean[][] tuppersMatrix() {
boolean[][] matrix = new boolean[17][106];
Arrays.stream(matrix).forEach( row -> Arrays.fill(row, true) );
final BigInteger seventeen = BigInteger.valueOf(17);
for ( int column = 0; column < 106; column++ ) {
for ( int row = 0; row < 17; row++ ) {
BigInteger y = k.add(BigInteger.valueOf(row));
BigInteger a = y.divideAndRemainder(seventeen)[0];
int bb = y.mod(seventeen).intValueExact();
bb += column * 17;
BigInteger b = BigInteger.TWO.pow(bb);
a = a.divideAndRemainder(b)[0];
int aa = a.mod(BigInteger.TWO).intValueExact();
matrix[row][105 - column] = ( aa == 1 );
}
}
return matrix;
}
private static final BigInteger k = new BigInteger("960939379918958884971672962127852754715"
+ "004339660129306651505519271702802395266424689642842174350718121267153782"
+ "770623355993237280874144307891325963941337723487857735749823926629715517"
+ "173716995165232890538221612403238855866184013235585136048828693337902491"
+ "454229288667081096184496091705183454067827731551705405381627380967602565"
+ "625016981482083418783163849115590225610003652351370343874461848378737238"
+ "198224849863465033159410054974700593138339226497249461751545728366702369"
+ "745461014655997933798537483143786841806593422227898388722980000748404719");
}
- Output:
Julia
import Plots: heatmap, savefig
""" get logical inverse of Tupper function values for black on background graphic """
function tupper_mat(k::BigInt)
tmatrix = trues(17, 106)
for (i, x) in enumerate(0:105), (j, y) in enumerate(k:k+16)
tmatrix[18 - j, 107 - i] = 1 != ((y ÷ 17) ÷ 2^(17x + (y % 17))) % 2
end
return tmatrix
end
const k = big"""960_939_379_918_958_884_971_672_962_127_852_754_715_004_339_660_129_306_651_505
519_271_702_802_395_266_424_689_642_842_174_350_718_121_267_153_782_770_623_355
993_237_280_874_144_307_891_325_963_941_337_723_487_857_735_749_823_926_629_715
517_173_716_995_165_232_890_538_221_612_403_238_855_866_184_013_235_585_136_048
828_693_337_902_491_454_229_288_667_081_096_184_496_091_705_183_454_067_827_731
551_705_405_381_627_380_967_602_565_625_016_981_482_083_418_783_163_849_115_590
225_610_003_652_351_370_343_874_461_848_378_737_238_198_224_849_863_465_033_159
410_054_974_700_593_138_339_226_497_249_461_751_545_728_366_702_369_745_461_014
655_997_933_798_537_483_143_786_841_806_593_422_227_898_388_722_980_000_748_404_719"""
function test_tupper()
bmap = tupper_mat(k)
display(heatmap(bmap, legend = :none, aspect_ratio = 1, ylims = [1,17]))
savefig("tupper.png")
end
test_tupper()
- Output:
![](http://static.miraheze.org/rosettacodewiki/b/bc/Tupper2.png)
Lua
Tested with Lua 5.4.6
Using Eduardo Bart's lua-bint pure Lua big integer library https://github.com/edubart/lua-bint.
The precision of the big integers must be specified in bits as a parameter of the require.
do -- 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 ) )
where x in 0..106, y in k..k+16
--]]
local bint = require 'lua-bint-master\\bint'(2048) -- need around 600 digits
local k = bint.fromstring( "960939379918958884971672962127852754715004339660129306651505" ..
"519271702802395266424689642842174350718121267153782770623355" ..
"993237280874144307891325963941337723487857735749823926629715" ..
"517173716995165232890538221612403238855866184013235585136048" ..
"828693337902491454229288667081096184496091705183454067827731" ..
"551705405381627380967602565625016981482083418783163849115590" ..
"225610003652351370343874461848378737238198224849863465033159" ..
"410054974700593138339226497249461751545728366702369745461014" ..
"655997933798537483143786841806593422227898388722980000748404" ..
"719" )
local b2 = bint.frominteger( 2 )
local b17 = bint.frominteger( 17 )
local kMod17 = bint.tointeger( k % b17 )
for yDelta = 0, 16 do
for x = 106, 0, -1 do
local powerOf2 = - ( 17 * x ) - ( kMod17 + yDelta ) % 17
-- if powerOf2 = 0, then ( v * 2 ^ powerOf2 ) mod 2 = v mod 2
-- if powerOf2 > 0, then ( v * 2 ^ powerOf2 ) mod 2 = 0
if powerOf2 > 0 then
io.write( " " )
else
local v = ( k + bint.frominteger( yDelta ) ) // b17
if powerOf2 < 0 then
v = v // bint.ipow( b2, bint.frominteger( - powerOf2 ) )
end
v = v % 2
io.write( ( bint.eq( v, 0 ) and " " ) or "#" )
end
end
io.write( "\n" )
end
end
- Output:
Same as Algol 68.
Mathematica /Wolfram Language
(*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}]
- Output:
Nim
import std/[algorithm, sugar]
import integers
let k = newInteger("960939379918958884971672962127852754715004339660129306651505519" &
"271702802395266424689642842174350718121267153782770623355993237" &
"280874144307891325963941337723487857735749823926629715517173716" &
"995165232890538221612403238855866184013235585136048828693337902" &
"491454229288667081096184496091705183454067827731551705405381627" &
"380967602565625016981482083418783163849115590225610003652351370" &
"343874461848378737238198224849863465033159410054974700593138339" &
"226497249461751545728366702369745461014655997933798537483143786" &
"841806593422227898388722980000748404719")
proc tuppersFormula(x, y: Integer): bool =
## Return true if point at (x, y) (x and y both start at 0)
## is to be drawn black, False otherwise
result = (k + y) div 17 div 2^(17 * x + y mod 17) mod 2 != 0
let values = collect:
for y in 0..16:
collect:
for x in 0..105:
tuppersFormula(x, y)
let f = open("tupper.txt", fmWrite)
for row in values:
for value in reversed(row): # x = 0 starts at the left so reverse the whole row.
f.write if value: "\u2588" else: " "
f.write '\n'
f.close()
- Output:
Same as Python.
Phix
You can run this online 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.
-- demo\rosetta\Tupper.exw requires("1.0.3") -- mpfr_fmod(), mpfr_si_pow_si() added include mpfr.e function make_bitmap() mpfr_set_default_precision(-8000) string kstr = """ 960_939_379_918_958_884_971_672_962_127_852_754_715_004_339_660_129_306_651_505 519_271_702_802_395_266_424_689_642_842_174_350_718_121_267_153_782_770_623_355 993_237_280_874_144_307_891_325_963_941_337_723_487_857_735_749_823_926_629_715 517_173_716_995_165_232_890_538_221_612_403_238_855_866_184_013_235_585_136_048 828_693_337_902_491_454_229_288_667_081_096_184_496_091_705_183_454_067_827_731 551_705_405_381_627_380_967_602_565_625_016_981_482_083_418_783_163_849_115_590 225_610_003_652_351_370_343_874_461_848_378_737_238_198_224_849_863_465_033_159 410_054_974_700_593_138_339_226_497_249_461_751_545_728_366_702_369_745_461_014 655_997_933_798_537_483_143_786_841_806_593_422_227_898_388_722_980_000_748_404_719""" mpz k = mpz_init(filter(kstr,"out","_\n")), y = mpz_init() mpfr half = mpfr_init(1/2), two = mpfr_init(2), t3 = mpfr_init() sequence bitmap = repeat(repeat(' ',106),17) for i=1 to 17 do mpz_add_si(y,k,i-1) integer r17 = mpz_fdiv_q_ui(y,y,17) for j=1 to 106 do mpfr_si_pow_si(t3,2,-17*floor(j-1)-r17) mpfr_mul_z(t3,t3,y) mpfr_fmod(t3,t3,two) mpfr_floor(t3,t3) if mpfr_cmp(half,t3)<0 then bitmap[i,j] = '#' end if end for end for return bitmap end function constant bitmap = make_bitmap() include pGUI.e Ihandle dlg, canvas cdCanvas cddbuffer, cdcanvas function redraw_cb(Ihandle /*canvas*/) integer {width, height} = IupGetIntInt(canvas,"RASTERSIZE"), ms = max(1,min(floor((width-109)/107),floor((height-19)/18))), mm = ms+1, cx = floor(width/2), cy = floor(height/2) cdCanvasActivate(cddbuffer) {} = cdCanvasMarkType(cddbuffer, CD_BOX) {} = cdCanvasMarkSize(cddbuffer, ms) for i=1 to 17 do for j=1 to 106 do if bitmap[i,j]='#' then cdCanvasMark(cddbuffer, cx+(53-j)*mm, cy+(8-i)*mm+ms) end if end for end for cdCanvasFlush(cddbuffer) return IUP_DEFAULT end function IupOpen() canvas = IupCanvas(Icallback("redraw_cb")) dlg = IupDialog(canvas,"RASTERSIZE=553x130, MINSIZE=231x76") IupSetAttribute(dlg, "TITLE", "Tupper's self-referential formula") IupMap(dlg) cdcanvas = cdCreateCanvas(CD_IUP, canvas) cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas) IupShow(dlg) if platform()!=JS then IupMainLoop() IupClose() end if
- Output:
At its smallest size on desktop/Phix:
![](http://static.miraheze.org/rosettacodewiki/f/f4/PhixTupper.png)
And the largest worth posting here, but it will go larger:
![](http://static.miraheze.org/rosettacodewiki/0/05/PhixTupperLarge.png)
Python
Original code programmed by the user halex [1]
#!/usr/bin/python
import codecs
import os
def tuppers_formula(x, y):
"""Return True if point (x, y) (x and y both start at 0) is to be drawn black, False otherwise
"""
k = 960939379918958884971672962127852754715004339660129306651505519271702802395266424689642842174350718121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719
return ((k + y)//17//2**(17*int(x) + int(y)%17))%2 > 0.5
with codecs.open("tupper.txt", "w", "utf-8") as f:
values = [[tuppers_formula(x, y) for x in range(106)] for y in range(17)]
for row in values:
for value in row[::-1]: # x = 0 starts at the left so reverse the whole row
if value:
f.write("\u2588") # Write a block
else:
f.write(" ")
f.write(os.linesep)
- Output:
The result is the file tupper.txt with the content:
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ ██ █ █ █ █ █ █ ██ ████ ███ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ ██ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ ██ ███ █ █ █ █ ███ ███ █ ███ ███ █ █ █ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ ████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ █ ██ ███ █ █ █ ██ █ ████ ████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ █ ███ ███ █ ███
Raku
# 20240802 Raku programming solution
my ( \H, \W, \N ) = 17, 106, 4858450636189713423582095962494202044581400587983244549483093085061934704708809928450644769865524364849997247024915119110411605739177407856919754326571855442057210445735883681829823754139634338225199452191651284348332905131193199953502413758765239264874613394906870130562295813219481113685339535565290850023875092856892694555974281546386510730049106723058933586052544096664351265349363643957125565695936815184334857605266940161251266951421550539554519153785457525756590740540157929001765967965480064427829131488548259914721248506352686630476300;
for N+H-1 ... N -> \y {
for ^W -> \x {
print 0.5 < (y div H) div 2**(H * x + y % H) % 2 ?? '█' !! ' '
}
say();
}
You may Attempt This Online!
Rust
use num::bigint::BigInt;
use num::{FromPrimitive, pow};
use std::primitive::bool;
/// Tupper function value maxtrix for graphic
fn tuppermat(kconst: BigInt) -> Vec<[bool; 106]> {
let mut tmatrix = vec![[true; 106]; 17];
let bigone: BigInt = BigInt::from_u32(1).unwrap();
let bigtwo: BigInt = BigInt::from_u32(2).unwrap();
for i in 0..106 {
for j in 0..17 {
let y: BigInt = kconst.clone() + j;
let mut a: BigInt = y.clone() / 17;
let mut b: BigInt = y.clone() % 17;
b += i * 17;
b = pow(bigtwo.clone(), b.try_into().unwrap());
a /= b;
a %= 2;
tmatrix[j][105 - i] = a == bigone;
}
}
return tmatrix
}
fn main() {
let k: BigInt = BigInt::parse_bytes(b"960939379918958884971672962127852754715004339660129306651505519271702802395266424689642842174350718121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719",
10).unwrap();
let bmap = tuppermat(k);
for line in bmap.iter() {
for c in line.iter() {
if *c {
print!("\u{2588}");
}
else {
print!(" ");
}
}
println!()
}
}
- Output:
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ ██ █ █ █ █ █ █ ██ ████ ███ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ ██ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ ██ ███ █ █ █ █ ███ ███ █ ███ ███ █ █ █ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ ████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ██ █ █ █ █ █ ██ ███ █ █ █ ██ █ ████ ████ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ █ ███ ███ █ ███
Wren
This works albeit rather slowly, taking almost 1.5 minutes to calculate the points to be plotted.
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.
import "dome" for Window
import "graphics" for Canvas, Color, Font
import "./plot" for Axes
import "./big" for BigRat
import "./iterate" for Stepped
var s = """
960 939 379 918 958 884 971 672 962 127 852 754 715 004 339 660 129 306 651 505
519 271 702 802 395 266 424 689 642 842 174 350 718 121 267 153 782 770 623 355
993 237 280 874 144 307 891 325 963 941 337 723 487 857 735 749 823 926 629 715
517 173 716 995 165 232 890 538 221 612 403 238 855 866 184 013 235 585 136 048
828 693 337 902 491 454 229 288 667 081 096 184 496 091 705 183 454 067 827 731
551 705 405 381 627 380 967 602 565 625 016 981 482 083 418 783 163 849 115 590
225 610 003 652 351 370 343 874 461 848 378 737 238 198 224 849 863 465 033 159
410 054 974 700 593 138 339 226 497 249 461 751 545 728 366 702 369 745 461 014
655 997 933 798 537 483 143 786 841 806 593 422 227 898 388 722 980 000 748 404
719
"""
s = s.replace(" ", "").replace("\n", "").replace("\r", "")
var k = BigRat.new(s)
var Pts = []
for (j in 0..16) {
var y = k + j
var t1 = (y/17).floor
var t2 = (y % 17).toInt
for (x in 0..106) {
var t3 = BigRat.two.pow(-17 * x - t2)
if (BigRat.half < ((t1 * t3) % 2).floor) {
Pts.add([106-x, 16-j])
System.print([j, x]) // to show progress on terminal
}
}
}
class Main {
construct new() {
Window.title = "Tupper's self-referential formula"
Canvas.resize(840, 260)
Window.resize(840, 260)
Canvas.cls(Color.white)
Font.load("Go-Regular9", "Go-Regular.ttf", 9)
Canvas.font = "Go-Regular9"
var axes = Axes.new(100, 200, 660, 180, -10..110, -1..17)
axes.draw(Color.black, 2)
var xMarks = Stepped.new(-10..110, 10)
var yMarks = Stepped.new(-1..17, 2)
axes.mark(xMarks, yMarks, Color.black, 2)
axes.label(xMarks, yMarks, Color.black, 2, Color.black)
axes.plot(Pts, Color.black, "█") // uses character 0x2588
}
init() {}
update() {}
draw(alpha) {}
}
var Game = Main.new()
![](http://static.miraheze.org/rosettacodewiki/c/cc/Wren_tupper.png)