小编eld*_*uth的帖子

为什么这个Ruby代码比同等的C++代码快得多?

最近我经历了一些简单的项目Euler问题并用Ruby和C++解决它们.但是对于有关Collat​​z猜想的问题14,我的C++代码在我终止它之前持续了大约半个小时,但是当我将代码翻译成Ruby时,它在9秒内解决了它.

这种差异让我感到难以置信 - 我一直认为C++几乎总是比Ruby快,特别是对于数学过程.

我的代码如下.

C++:

#include <iostream>

using namespace std;

int main ()
{
    int a = 2;
    int b = 2;
    int c = 0;
    while (b < 1000000)
    {

        a = b;
        int d = 2;
        while (a != 4)
        {
            if (a % 2 == 0)
                a /= 2;
            else
                a = 3*a + 1;
            d++;
        }
        if (d > c)
        {
            cout << b << ' ' << d << endl;
            c=d;
        } …
Run Code Online (Sandbox Code Playgroud)

c++ math

20
推荐指数
1
解决办法
1024
查看次数

c ++不正确的浮点运算

对于以下程序:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    for (float a = 1.0; a < 10; a++)
        cout << std::setprecision(30) << 1.0/a << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下输出:

1
0.5
0.333333333333333314829616256247
0.25
0.200000000000000011102230246252
0.166666666666666657414808128124
0.142857142857142849212692681249
0.125
0.111111111111111104943205418749
Run Code Online (Sandbox Code Playgroud)

对于较低位的数字,这绝对不是正确的,特别是对于1/3,1/5,1/7和1/9.事情刚开始出错10 ^ -16我希望看到更多类似的东西:

1
0.5
0.333333333333333333333333333333
0.25
0.2
0.166666666666666666666666666666
0.142857142857142857142857142857
0.125
0.111111111111111111111111111111
Run Code Online (Sandbox Code Playgroud)

这是float类中的继承缺陷吗?有没有办法克服这个并进行适当的划分?是否有用于执行精确十进制运算的特殊数据类型?在我的例子中,我只是做了一些愚蠢或错误的事情吗?

c++ floating-point division

6
推荐指数
2
解决办法
6826
查看次数

迭代数组(Project Euler#23)

我有以下代码

#!/usr/bin/ruby -w
c = 1
d = Array.new(6965)  #6965 is the amount of abundant numbers below 28123 of which all numbers greater than that can be written as the sum of two abundant numbers
f = 0
while c < 28124      # no need to go beyond 28123 for this problem
  a = 0
  b = 1
  i = true           # this will be set to false if a number can be written as the sum of two abundant …
Run Code Online (Sandbox Code Playgroud)

ruby arrays iteration

1
推荐指数
1
解决办法
277
查看次数

为简单项目返回错误的值

我一直在为编程实践网站解决一个问题,我已经在ruby中解决了这个并且很容易返回正确的值,问题是找到可以从三位数的乘法产生的最大的回文数,我的代码是如下

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int h = 0;                                      //this will hold the largest palindrome
    for (int a = 101; a < 1000; a++)                //will cycle through all three digit numbers
    {
        for (int b = 101; b < 1000; b++)            //will cycle through all three digit numbers
        {
            if (a*b%10 != 0)                        //checks to make sure last digit is not zero
            {
                int c = a*b;                        //temporary int to hold value when it …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
103
查看次数

在红宝石中找到位置值

我正在尝试研究这个简单的操作,而我却没有为它做任何事情.我希望能够找到一个整数的位置值,我想知道是否有特定的宝石或操作.例如:

a = 1651684651
p find_place_value_of(a,5) # imaginary function to return the value of the
                           #  number in the 10000 column
                           #  output should be 8
Run Code Online (Sandbox Code Playgroud)

到目前为止,我能做的最好的事情是想出这个丑陋的小功能:

j= 262322
a= j 
a/=100000 
b= j - a*100000
b/=10000 
c= j - a*100000 - b*10000 
c/=1000 
d= j - a*100000 - b*10000 - c*1000 
d/=100 
e= j - a*100000 - b*10000 - c*1000 - d*100
e/=10 
f= j - a*100000 - b*10000 - c*1000 - d*100 - e*10
p a,b,c,d,e,f,j
Run Code Online (Sandbox Code Playgroud)

是否有更优雅的方式来寻找地方价值?

ruby math

0
推荐指数
1
解决办法
218
查看次数

标签 统计

c++ ×3

math ×2

ruby ×2

arrays ×1

division ×1

floating-point ×1

iteration ×1