本文介绍如何使用JavaScript在HTML页面中实现角谷猜想的二进制运算过程,便于在浏览器中直接观察和研究其规律。相比其他环境,在手机浏览器上也能运行的HTML/JS方案无疑更为便捷。
以下是完整的实现代码,包含三个不同输出格式的函数:
<html>
<head></head>
<script>
window.currentPostId = 8309276;
window.currentPostDateAdded = '2018-01-18 11:25';
function run(){
// onclick="alert('Hello world!')"
// alert('Hello world!');
var num=document.getElementById("num").value;
// alert(num);
// document.getElementById("listnum").value=num+"\r"+num;
var num10= parseInt(num,2);
var num2=num10.toString(2);//
var rowid=1;
var rowidstr=(" "+rowid).slice(-3); //slice()方法
var flagstr=(" ").substr(0,6);
var num10str=(" "+num10).slice(-6); //slice()方法
var num2str=(" "+num2).slice(-30); //slice()方法
var num22str=(num2+" ").substr(0,30)+" ::";
document.getElementById("listnum").value=num22str+rowidstr+"::"+flagstr+"::"+num10str+"::"+num2str;
while (num10!=1){
num10=num10*3;
num10=num10+1;
// if (num % 2 === 0)
while ( num10 % 2 == 0){
num10=num10 / 2;
}
num2=num10.toString(2);
rowid=rowid+1;
rowidstr=(" "+rowid).slice(-3); //slice()方法
flagstr=("/2^n= ").substr(0,6);
num10str=(" "+num10).slice(-6); //slice()方法
num2str=(" "+num2).slice(-30); //slice()方法
num22str=(num2+" ").substr(0,30)+" ::";
document.getElementById("listnum").value= document.getElementById("listnum").value+"\r"+num22str+rowidstr+"::"+flagstr+"::"+num10str+"::"+num2str;
}
}
function run2(){
var num=document.getElementById("num").value;
var num10= parseInt(num,2);
var num2=num10.toString(2);//
var rightnum=0;
var rightstr=" ";
var rowid=1;
var rowidstr=(" "+rowid).slice(-3); //slice()方法
var flagstr=(" ").substr(0,6);
var num10str=(" "+num10).slice(-6); //slice()方法
var num22str="";
while (num10!=1){
num10=num10*3;
num10=num10+1;
while ( num10 % 2 == 0){
num10=num10 / 2;
rightnum=rightnum+1;
}
}
var leftstr="w";
var i=0;
while (i<rightnum){
leftstr=leftstr+" ";
i=i+1;
}
var maxlength=rightnum;
rightnum=0;
num10= parseInt(num,2);
num2=num10.toString(2);//
rightstr=" ";
rowid=1;
rowidstr=(" "+rowid).slice(-3); //slice()方法
flagstr=(" ").substr(0,6);
num10str=(" "+num10).slice(-6); //slice()方法
num22str="";
num22str=(leftstr+num2+rightstr).slice((-1)*maxlength)+"::";
document.getElementById("listnum").value=num22str+rowidstr+"::"+flagstr+"::"+num10str;
while (num10!=1){
num10=num10*3;
num10=num10+1;
while ( num10 % 2 == 0){
num10=num10 / 2;
rightnum=rightnum+1;
rightstr=rightstr+" ";
}
num2=num10.toString(2);
rowid=rowid+1;
rowidstr=(" "+rowid).slice(-3); //slice()方法
flagstr=("3N12= ").substr(0,6);
num10str=(" "+num10).slice(-6); //slice()方法
//alert(num2);
num22str=(leftstr+num2+rightstr).slice((-1)*maxlength)+"::";
document.getElementById("listnum").value= document.getElementById("listnum").value+"\r"+num22str+rowidstr+"::"+flagstr+"::"+num10str;
}
}
function run3(){
var num=document.getElementById("num").value;
//var num10= parseInt(num,2);
var num10= num;
//var num2=num10.toString(2);//
var num2=num10;//
var rightnum=0;
var rightstr=" ";
var rowid=1;
var rowidstr=(" "+rowid).slice(-3); //slice()方法
var flagstr=(" ").substr(0,6);
var num10str=(" "+num10).slice(-6); //slice()方法
var num22str="";
// alert(num10.length);//从0到length-1
// alert(num10[num10.length-4]);
//while (num10!=1){
var num10n3=num10;
var i=0;
var jjj=0;
var temp="";
while (num10!="1"){
temp=num10;
num10="00000"+temp;
num10n3="0000"+temp+"0";
i=num10.length;
temp="";
jjj=0;
//百度:因为在JavaScript中字符串是不可变的(immutable),这意味着你不能改变字符串的某个字符
while (i>=0){
//alert(num10+"++++"+num10n3+"===="+temp);
//alert("i="+i+"num10[i]="+num10[i]+";;;num10n3[i]="+num10n3[i]+";;;jjj="+jjj);
if (jjj==0){
if (num10[i]=='0' && num10n3[i]=='0'){
temp='0'+temp;
jjj=0;
}
if (num10[i]=='0' && num10n3[i]=='1'){
temp='1'+temp;
jjj=0;
}
if (num10[i]=='1' && num10n3[i]=='0'){
temp='1'+temp;
jjj=0;
//alert(num10n3);
}
if (num10[i]=='1' && num10n3[i]=='1'){
temp='0'+temp;
jjj=1;
}
}else{///jjj=1
if (num10[i]=='0' && num10n3[i]=='0'){
temp='1'+temp;
jjj=0;
}
if (num10[i]=='0' && num10n3[i]=='1'){
temp='0'+temp;
jjj=1;
}
if (num10[i]=='1' && num10n3[i]=='0' ){
temp='0'+temp;//ttemp='0'+temp;这里写错多一个t,ttemp,不报错,查错烦不顺
jjj=1;
}
if (num10[i]=='1' && num10n3[i]=='1'){
temp='1'+temp;
jjj=1;
}
}
i=i-1;
}
//alert("*3="+temp);
// num10=num10*3;
// num10=num10+1;
num10n3=temp;
temp="";
i=num10n3.length-1;
jjj=1;
while (i>=0){
if (jjj==0){
if (num10n3[i]=='0'){
temp='0'+temp;
jjj=0;
}
if (num10n3[i]=='1'){
temp='1'+temp;
jjj=0;
}
}else{///jjj=1
if ( num10n3[i]=='0'){
temp='1'+temp;
jjj=0;
}
if ( num10n3[i]=='1'){
temp='0'+temp;
jjj=1;
}
}
i=i-1;
//alert("+1="+temp);
}
//alert("+1="+temp);
num10n3=temp;
temp="";
while ( num10n3[num10n3.length-1]=='0'){
temp=num10n3.substring(0,num10n3.length-1);
num10n3=temp;
//alert(num10n3);
//alert("tail="+num10n3[num10n3.length-1]);
rightnum=rightnum+1;
}
//alert("hhh="+num10n3[0]);
while ( num10n3[0]=='0'){
num10n3=num10n3.substring(1,num10n3.length);
//alert("hhhttt="+num10n3);
}
num10=num10n3;
//alert(rightnum);
//alert(num10n3);
}
//alert(rightnum);
//alert(num10n3);
var leftstr=" ";
var i=0;
while (i<rightnum){
leftstr=leftstr+" ";
i=i+1;
}
var maxlength=rightnum;
rightnum=0;
num10= num;
num2=num10;//
rightstr=" ";
rowid=1;
rowidstr=(" "+rowid).slice(-3); //slice()方法
flagstr=("原数 ").substr(0,6);
//num10str=(" "+num10).slice(-6); //slice()方法
num10str="十进制";
//alert(maxlength);
num22str="";
num2=num;
num22str=(leftstr+num2+rightstr).slice((-1)*maxlength-10)+"::";
document.getElementById("listnum").value=num22str+rowidstr+"::"+flagstr+"::"+num10str;
rightnum=0;
//////////////////////////////////////////////////////////////
//if (false){
if (true){
while (num10!="1"){
temp=num10;
num10="00000"+temp;
num10n3="0000"+temp+"0";
i=num10.length;
temp="";
jjj=0;
//百度:因为在JavaScript中字符串是不可变的(immutable),这意味着你不能改变字符串的某个字符
while (i>=0){
//alert(num10+"++++"+num10n3+"===="+temp);
//alert("i="+i+"num10[i]="+num10[i]+";;;num10n3[i]="+num10n3[i]+";;;jjj="+jjj);
if (jjj==0){
if (num10[i]=='0' && num10n3[i]=='0'){
temp='0'+temp;
jjj=0;
}
if (num10[i]=='0' && num10n3[i]=='1'){
temp='1'+temp;
jjj=0;
}
if (num10[i]=='1' && num10n3[i]=='0'){
temp='1'+temp;
jjj=0;
//alert(num10n3);
}
if (num10[i]=='1' && num10n3[i]=='1'){
temp='0'+temp;
jjj=1;
}
}else{///jjj=1
if (num10[i]=='0' && num10n3[i]=='0'){
temp='1'+temp;
jjj=0;
}
if (num10[i]=='0' && num10n3[i]=='1'){
temp='0'+temp;
jjj=1;
}
if (num10[i]=='1' && num10n3[i]=='0' ){
temp='0'+temp;//ttemp='0'+temp;这里写错多一个t,ttemp,不报错,查错烦不顺
jjj=1;
}
if (num10[i]=='1' && num10n3[i]=='1'){
temp='1'+temp;
jjj=1;
}
}
i=i-1;
}
// alert("*3="+temp);
// num10=num10*3;
// num10=num10+1;
num10n3=temp;
temp="";
i=num10n3.length-1;
jjj=1;
while (i>=0){
if (jjj==0){
if (num10n3[i]=='0'){
temp='0'+temp;
jjj=0;
}
if (num10n3[i]=='1'){
temp='1'+temp;
jjj=0;
}
}else{///jjj=1
if ( num10n3[i]=='0'){
temp='1'+temp;
jjj=0;
}
if ( num10n3[i]=='1'){
temp='0'+temp;
jjj=1;
}
}
i=i-1;
}
//alert("+1="+temp);
num10n3=temp;
temp="";
//rightnum=0;
while ( num10n3[num10n3.length-1]=='0'){
temp=num10n3.substring(0,num10n3.length-1);
num10n3=temp;
//alert(num10n3);
//alert("tail="+num10n3[num10n3.length-1]);
rightnum=rightnum+1;
}
//alert("hhh="+num10n3[0]);
while ( num10n3[0]=='0'){
num10n3=num10n3.substring(1,num10n3.length);
//alert("hhhttt="+num10n3);
}
num10=num10n3;
i=0;
rightstr=" ";
while (i<rightnum){
rightstr=rightstr+" ";
i=i+1;
}
leftstr=" ";
//leftstr="";
i=0;
while (i<maxlength){
leftstr=leftstr+" ";
i=i+1;
}
//leftstr=leftstr;
//alert("maxlength="+maxlength);
//alert("leftstr="+leftstr+"w");
num2=num10;
rowid=rowid+1;
rowidstr=(" "+rowid).slice(-3); //slice()方法
flagstr=("3N12= ").substr(0,6);
num10str=("十进制").slice(-6); //slice()方法
//alert(num2);
num22str=(leftstr+num2+rightstr).slice((-1)*maxlength-10)+"::";
document.getElementById("listnum").value= document.getElementById("listnum").value+"\r"+num22str+rowidstr+"::"+flagstr+"::"+num10str;
}
}
}
////太长的话,暂时没办法的
////太长的话,暂时没办法的
</script>
<body class="skin-anothereon001 has-navbar has-bannerbar">
<div id="top_nav" class="navbar forpc">
<input type="text" id="num" value="1111111111" style="width:600px" />
<br />
<input type="button" name="run" value="二进制" style="width:70px" onclick="run3();" />
<input type="button" name="run" value="1" style="width:70px" onclick="run();" />
<input type="button" name="run" value="2" style="width:70px" onclick="run2();" />
<br />
<div id="top_nav" style="width: 1500px;height: 2000px;overflow-y: auto; overflow-x: auto; ">
<textarea rows="1200" cols="2580" id="listnum"> </textarea>
</div>
</div>
</body>
</html>
功能说明与输出示例
页面提供了一个输入框(默认值为二进制 1111111111)和三个按钮:“二进制”、“1”、“2”。
- 按钮“1”(
run()函数):以标准格式输出每一步的十进制和二进制结果。
- 按钮“2”(
run2()函数):输出格式侧重展示除以2的幂次过程。
- 按钮“二进制”(
run3()函数):核心函数。它完全在二进制字符串层面模拟“乘3加1”和除以2的操作,而不借助JavaScript的数值运算,直接展示二进制位的变化过程。这对于深入理解猜想在二进制下的行为模式很有帮助。
例如,点击“二进制”按钮,程序会逐步计算并输出类似下面的对齐格式(通过 textarea 显示),清晰展示了二进制数在变换过程中的位移动画:



总结
这段代码提供了一个直观的前端工具,将角谷猜想这个数论问题通过二进制视角呈现出来。run3() 函数的实现尤其具有启发性,它避免了直接的大数运算,通过字符串操作模拟二进制计算,适合用于教学演示或对算法本身感兴趣的研究者。
你可以将完整代码保存为 .html 文件并在浏览器中打开,自行输入不同的二进制数进行测试和观察。希望这个实现能帮助你从另一个角度探索角谷猜想的奥秘。如果你对这类算法实现或底层计算机原理有更多兴趣,欢迎在 云栈社区 交流讨论。
|