简体   繁体   English

如何使该质数表反向显示

[英]How to make this table of prime numbers display in reverse

I have created a table that displays all prime numbers from 2-1013 but it is displaying from the bottom right to the top left and I would like it to display from the top left to the bottom right. 我创建了一个表,该表显示2-1013的所有素数,但是它是从右下角到左上角显示的,我希望它从左上角到右下角显示。 How would I achieve this? 我将如何实现?

<!DOCTYPE HTML Public>
<html>

    <head>
        <title>Prime Numbers</title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    </head>

    <body onload='CalcPrime()'>
        <script type='text/javascript'>
            function CalcPrime() {
                var setIt = 0;
                var count = 0;
                var num = 3;
                var primeArray = new Array();
                primeArray.unshift(2)

                while (count < 169) {
                    setIt = 0;
                    var i = 2

                    while (i < num + 1) {
                        if (num % i == 0) {
                            setIt = setIt + 1;
                        }
                        i = i + 1
                    }
                    if (setIt < 2) {
                        primeArray.unshift(num);
                        count = count + 1;
                    }
                    num = num + 1;
                }
                var a;
                document.write("<table cols='10' border='1'>");
                document.write("<tr><th colspan=10 border='1'>Prime Numbers 2-1013</th></tr>");
                for (var it = 0; it < 161; it = it + 10) {
                    document.write("<tr>");
                    for (var colm = 0; colm < 10; colm++) {
                        a = it + colm;
                        document.write("<td style='border:1px line;padding:10px;'>" + primeArray[a] + "</td>");
                    }
                }
            }
        </script>
    </body>

</html>

This will get you close. 这将使您接近。 Basically, instead if incrementing your indices, decrement them. 基本上,如果增加索引,则减少它们。 Instead of starting with 0, start with the final number and go down to zero. 而不是从0开始,而是从最终数字开始,然后下降到零。 In other words, reverse your loop. 换句话说,反转循环。

for (var it = 161; it >= 0; it -= 10) {
    document.write("<tr>");
    for (var colm = 10; colm >= 0; colm--) {
        a = it + colm;
        document.write("<td style='border:1px line;padding:10px;'>" + primeArray[a] + "</td>");
    }
}

It's not perfect yet, but this will get you close for now. 尚不完美,但这将使您暂时关闭。 I'll edit after I tweak it a bit. 稍作调整后,我将进行编辑。

在写表之前,通过添加以下内容来反转primeArray变量:

primeArray = primeArray.reverse();

If you push your values onto the array instead of unshifting them, that will put them at the end of your array instead of the start. 如果将值推入数组而不是不移动它们,这会将它们放在数组的末尾而不是开始。 Which would do exactly what you want. 哪个可以完全满足您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM