[英]Tupper’s Self-Referential Formula in C++ not working as expected?
I'm trying to write Tuppers Formula in C++ using TTMath to store the big integer. 我正在尝试使用TTMath在C ++中编写Tuppers公式来存储大整数。 But my output is just a jumbled mess instead of the picture it's supposed to show.
但是我的输出只是一团糟,而不是应该显示的图片。 I can't wrap my head around what is causing this.
我无法解决造成这种情况的原因。
#include <iostream>
#include <fstream>
#include <ttmath/ttmath.h>
int main(int argsm, int** args)
{
const ttmath::Int<1> HEIGHT = 17;
const ttmath::Int<1> WIDTH = 106;
ttmath::Big<300, 300> a, b, c, d, e, f, g, h, i, j, k, l, y;
std::ofstream newFile;
char matrix[107][18];
newFile.open("image.txt");
for (ttmath::UInt<3> x = 0; x < WIDTH; ++x)
{
y = ttmath::Big<1, 200>("9609393799189588849716729621278527547150043396601293066515055192717028023952664246896428421743507 18121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719");
for (ttmath::UInt<2> v = 0; v <= HEIGHT; ++v)
{
k = y;
a = y;
a.Mod(17);
b = a;
c = x;
c.Mul(17);
b.Mul(1);
d = c + b;
d.Pow(2);
e = d;
k.Div(17);
f = k ;
g = f;
g.Div(e);
h = g;
h.Mod(2);
j = h;
j > 0.5 ? matrix[stoi(x.ToString())][stoi(v.ToString())] = '\u2588' : matrix[stoi(x.ToString())][stoi(v.ToString())] = ' ';
y.Add(1);
}
}
for (signed int h = 0; h < std::stoi(HEIGHT.ToString()); ++h)
{
for (signed int w = 0; w < std::stoi(WIDTH.ToString()); ++w)
{
newFile << matrix[w][h];
}
newFile << "\n";
}
newFile.close();
return 0;
}
Apart from some other issues pointed out in the comments, the main problem appears to be that the line 除了评论中指出的其他一些问题外,主要问题似乎是该行
d.Pow(2);
calculates d^2
, when what you want is 2^d
. 当您想要的是
2^d
时,计算d^2
。
Regarding your second question in your comment: In order to flip the resulting image upside down, all you have to do is use 关于您评论中的第二个问题:为了将生成的图像上下颠倒,您所要做的就是使用
newFile << matrix[w][HEIGHT-h-1];
in your output line. 在您的输出行中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.