Tupper's self-referential formula: Difference between revisions

m
m (→‎{{header|Wren}}: Minor alteration needed for this to run on Windows.)
 
(45 intermediate revisions by 12 users not shown)
Line 1:
{{draft task}}
 
;Introduction
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 27:
:* Wikipedia: [[wp:Tupper's self-referential formula|Tupper's self-referential formula]]
:* [http://www.dgp.toronto.edu/~mooncake/papers/SIGGRAPH2001_Tupper.pdf Tupper, Jeff. "Reliable Two-Dimensional Graphing Methods for Mathematical Formulae with Two Free Variables"]
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT which has programmer specifiable precision.<br>
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">
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
</syntaxhighlight>
{{out}}
<pre>
# # # ## # # # # # # # ## # # #
# # # # # # # # # # # # # # # # #
## # # # # ## # # # # # # ## #### ### ### # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # ### ### # # # # # # # # #
# # # # # # # ## # # # # # # # # ## # #
### # # # # # # # # # # # # # # # # # # # #
# # ## # ## ### # # # # ### ### # ### ### # # # # #
### # # # # # # # # # # # #### # # # # #
# # # # # # # # # # # # # # # #
## # # # # # ## ### # # # ## # #### #### # #
# # # # # # # # # #
# # # # # # # # # #
# # # # # # # # # #
### # # # # # # # #
# # # # # #
### # ### ### # ###
</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>
 
=={{header|Fōrmulæ}}==
Line 46 ⟶ 243:
[[File:Fōrmulæ - Tupper's self-referential formula 03.png]]
 
'''Visualization as a matrix.''' The following creates a 17×10717×106 matrix. Values for which inequality is true are shown as an opaque black color, otherwise they are shown as an transparent color.
 
[[File:Fōrmulæ - Tupper's self-referential formula 04.png]]
 
(image is resized 50% of its original)
[[File:Fōrmulæ - Tupper's self-referential formula 05.png]]
 
[[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:
Line 58 ⟶ 257:
[[File:Fōrmulæ - Tupper's self-referential formula 07.png]]
 
[[File:Fōrmulæ - Tupper's self-referential formula 08.png]]
=={{header|Julia}}==
<syntaxhighlight lang="julia">import Images: colorview, Gray
import Plots: plot
 
=={{header|J}}==
setprecision(BigFloat, 8000)
'''Tacit Solution'''
<syntaxhighlight lang="j">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.</syntaxhighlight>
 
Example use:
<syntaxhighlight lang="j"> 17 Tupperimage 106</syntaxhighlight>
 
Produces the following text-based image,
 
{{out}}
<pre>
* * * ** * * * * * * * ** * * *
* * * * * * * * * * * * * * * * *
** * * * * ** * * * * * * ** **** *** *** * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *** *** * * * * * * * * *
* * * * * * * ** * * * * * * * * ** * *
*** * * * * * * * * * * * * * * * * * * * *
* * ** * ** *** * * * * *** *** * *** *** * * * * *
*** * * * * * * * * * * * **** * * * * *
* * * * * * * * * * * * * * * *
** * * * * * ** *** * * * ** * **** **** * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
*** * * * * * * * *
* * * * * *
*** * *** *** * ***
</pre>
 
The code is tacit and fixed (in other words, it is point-free):
 
<syntaxhighlight lang="j"> _80 [\ (5!:5) <'Tupperimage'
{&' *'@:([ (] (1r2 < <.@:(2 | (17 %~ ]) * 2 ^ (_17 * <.@:[) - 17x | <.@:])) 9609
39379918958884971672962127852754715004339660129306651505519271702802395266424689
64284217435071812126715378277062335599323728087414430789132596394133772348785773
57498239266297155171737169951652328905382216124032388558661840132355851360488286
93337902491454229288667081096184496091705183454067827731551705405381627380967602
56562501698148208341878316384911559022561000365235137034387446184837873723819822
48498634650331594100549747005931383392264972494617515457283667023697454610146559
97933798537483143786841806593422227898388722980000748404719x"_ + [)"0/ |.@:])&:i
.</syntaxhighlight>
 
Alternatively,
<syntaxhighlight lang="j">Tuppergraph=. viewmat o ([ load o ('viewmat'"_)) o ([ (] inequality K + [)"0/ |. o ])&:i.
 
17 Tuppergraph 106</syntaxhighlight>
 
produces the following graph,
 
[[File:J Tupper graph.png]]
 
=={{header|Java}}==
<syntaxhighlight lang="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");
}
</syntaxhighlight>
{{ out }}
[[Media:TuppersJava.png]]
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">import Plots: heatmap, savefig
 
""" get logical inverse of Tupper function values for black on whilebackground graphic """
function tupper_mat(k::BigInt)
tmatrix = trues(17, 106)
for (i, x) in enumerate(0.0:1:105), (j, y) in enumerate(k:k+16)
tmatrix[18 - j, 107 - i] = 1/2 >!= floor(mod(floor(y /÷ 17) *÷ 2^(-1717x *+ floor(x)y - mod(floor(y),% 17)),) % 2))
end
return tmatrix
Line 85 ⟶ 409:
function test_tupper()
bmap = tupper_mat(k)
display(plotheatmap(colorview(Graybmap, bmap)legend = :none, aspect_ratio = 1, ylims = [1,17]))
savefig("tupper.png")
end
Line 92 ⟶ 416:
</syntaxhighlight>{{out}}
 
[[File:TupperTupper2.png|center]]
 
=={{header|Lua}}==
Tested with Lua 5.4.6
{{Trans|Algol 68}}
Using Eduardo Bart's lua-bint pure Lua big integer library https://github.com/edubart/lua-bint.<br>
The precision of the big integers must be specified in bits as a parameter of the require.
<syntaxhighlight lang="lua">
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
</syntaxhighlight>
{{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}}==
{{trans|python}}
{{libheader|integers}}
<syntaxhighlight lang="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()
</syntaxhighlight>
 
{{out}}
Same as Python.
 
=={{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)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Tupper.exw</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.3"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- mpfr_fmod(), mpfr_si_pow_si() added</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">make_bitmap</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">8000</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">kstr</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
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"""</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">kstr</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"out"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"_\n"</span><span style="color: #0000FF;">)),</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">half</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">two</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">t3</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">bitmap</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">106</span><span style="color: #0000FF;">),</span><span style="color: #000000;">17</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">17</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">mpz_add_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">r17</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_fdiv_q_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">17</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">106</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">mpfr_si_pow_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">17</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">r17</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_mul_z</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_fmod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">two</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t3</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpfr_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">half</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t3</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">bitmap</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'#'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">bitmap</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">bitmap</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">make_bitmap</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*canvas*/</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">ms</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">width</span><span style="color: #0000FF;">-</span><span style="color: #000000;">109</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">107</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">height</span><span style="color: #0000FF;">-</span><span style="color: #000000;">19</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">18</span><span style="color: #0000FF;">))),</span>
<span style="color: #000000;">mm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ms</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">cy</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">height</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCanvasMarkType</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BOX</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCanvasMarkSize</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ms</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">17</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">106</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bitmap</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'#'</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">cdCanvasMark</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">53</span><span style="color: #0000FF;">-</span><span style="color: #000000;">j</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">mm</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cy</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">8</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">mm</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ms</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE=553x130, MINSIZE=231x76"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Tupper's self-referential formula"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupMap</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
{{out}}
At its smallest size on desktop/Phix:
[[File:PhixTupper.png|center]]
And the largest worth posting here, but it will go larger:
[[File:PhixTupperLarge.png|center]]
 
=={{header|Python}}==
Original code programmed by the user halex
[https://stackoverflow.com/questions/29805197/imprecise-floats-in-tuppers-self-referential-formula]
<syntaxhighlight lang="python">#!/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)</syntaxhighlight>
{{out}}
The result is the file tupper.txt with the content:
<pre style="font-size: .75em;">
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
██ █ █ █ █ ██ █ █ █ █ █ █ ██ ████ ███ ███ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ ███ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ ██ █ █
███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ ██ █ ██ ███ █ █ █ █ ███ ███ █ ███ ███ █ █ █ █ █
███ █ █ █ █ █ █ █ █ █ █ █ ████ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
██ █ █ █ █ █ ██ ███ █ █ █ ██ █ ████ ████ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
███ █ █ █ █ █ █ █ █
█ █ █ █ █ █
███ █ ███ ███ █ ███
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="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!()
}
}
</syntaxhighlight>{{out}}
<pre>
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
██ █ █ █ █ ██ █ █ █ █ █ █ ██ ████ ███ ███ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ ███ ███ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ ██ █ █ █ █ █ █ █ █ ██ █ █
███ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
█ █ ██ █ ██ ███ █ █ █ █ ███ ███ █ ███ ███ █ █ █ █ █
███ █ █ █ █ █ █ █ █ █ █ █ ████ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
██ █ █ █ █ █ ██ ███ █ █ █ ██ █ ████ ████ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █
███ █ █ █ █ █ █ █ █
█ █ █ █ █ █
███ █ ███ ███ █ ███
</pre>
 
=={{header|Wren}}==
Line 99 ⟶ 731:
{{libheader|Wren-big}}
{{libheader|Wren-iterate}}
{{libheader|Go-fonts}}
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.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color, Font
import "./plot" for Axes
import "./big" for BigRat
Line 144 ⟶ 777:
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)
Line 150 ⟶ 785:
axes.mark(xMarks, yMarks, Color.black, 2)
axes.label(xMarks, yMarks, Color.black, 2, Color.black)
axes.plot(Pts, Color.black, "") // uses character 0x25A00x2588
}
 
2,120

edits