Tupper's self-referential formula: Difference between revisions

m
 
(32 intermediate revisions by 11 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">
BEGIN #CO plot Tupper's self-referential formula #
CO 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
Line 57 ⟶ 56:
# 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 #
LONG LONG INT v := IF power of 2 > 0 THEN 0 ELSE( k + y delta ) OVER 17 FI;
IF power of 2 > 0 THEN
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 92 ⟶ 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 117 ⟶ 248:
 
(image is resized 50% of its original)
 
[[File:Fōrmulæ - Tupper's self-referential formula 05.png|1070px]]
 
Line 124 ⟶ 256:
 
[[File:Fōrmulæ - Tupper's self-referential formula 07.png]]
 
[[File:Fōrmulæ - Tupper's self-referential formula 08.png]]
 
=={{header|J}}==
'''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}}==
Line 159 ⟶ 419:
 
=={{header|Lua}}==
Tested with Lua 5.4.56
{{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 bint = require 'bint'(2048) -- need around 600 digits
 
local k = bint.fromstring( "960939379918958884971672962127852754715004339660129306651505" ..
Line 183 ⟶ 442:
"719" )
 
local kMod17 = bint.tointeger( k % 17 )
local b17 = bint.frominteger( 17 )
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
Line 208 ⟶ 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}}==
{{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)-->
Line 317 ⟶ 644:
{{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>
█ █ █ ██ █ █ █ █ █ █ █ ██ █ █ █
Line 342 ⟶ 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 387 ⟶ 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 393 ⟶ 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