找出最少的ramanujan数字.. [英] find out least ramanujan number..

查看:113
本文介绍了找出最少的ramanujan数字..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ramanujan数字:表示为两个或更多不同数字的多维数据集总和的数字。

ramanujan number:a number expressed as sum of cubes of two or more different numbers.

#include<stdio.h>
int main()
{
    int a,b,i,count=1;
    for(i=1; i<=10000; i++)
    {
        for(a=1; a<=10000; a++)
        {
            for(b=1; b<=10000; b++)
            {
                if(i==a*a*a+b*b*b)
                {
                    count++;
                }
                if(count>=2)
                {
                    printf("%d",i);
                }
                break;
            }
            break;
        }
        break;
    }
    return 0;
}





enhzflep:代码格式



enhzflep: code formatted

推荐答案

您没有问过具体问题。但是你的代码将无法按预期工作。



您正在使用整数计算,当结果大于可以表示的最大数字时,数学运算将失败。因此,您必须相应地限制算法。



使用 int ,最大数字定义为<$ c $ limits.h 包含文件中的c> INT_MAX 。这个值通常是2147483647.所以你的算法必须限制在低于立方根的循环变量1290.16。



如果你需要更大的循环变量,你必须使用更大的数据类型,如 long long 。这通常是64位整数(参见 LLONG_MAX ),其中立方根是2097152。
You did not ask a specific question. But your code will not work as expected.

You are using integer calculations where mathematical operations will fail when the result is larger than the largest number that can be represented. So you must limit your algorithm accordingly.

With int, the largest number is defined as INT_MAX in the limits.h include file. This value is usually 2147483647. So your algorithm must be limited to loop variables that are below the cube root which is 1290.16.

If you need larger loop variables, you must use a larger data type like long long. That are usually 64-bit integers (see LLONG_MAX) where the cube root is 2097152.


如上所述,将无法工作在第一次迭代之后,becuas eyou正在打破循环。删除休息时间,并考虑将其放入函数中,以便在匹配值时返回。



但是...当你修复它时,可能需要一些执行的时间......相当多的时间。

您正在执行内部if条件1,000,000,000,000次 - 在我去吃晚餐之前,这不太可能在我的计算机上完成。 />


我认为你需要重新考虑你的算法 - 这里的蛮力和无知方法不是一个好策略。
As written, than won't work at all, becuas eyou are "break"ing out of the loops after the first iteration. Removes the breaks, and consider putting it into a function so you can return when you match your values.

But...when you fix it, that may take some time to execute...quite a lot of time.
You are executing the inner "if" conditions 1,000,000,000,000 times - which is unlikely to complete on my computer before I go off for my supper.

I think you need to reconsider your algorithm - the "brute force and ignorance" approach is not a good strategy here.


这篇关于找出最少的ramanujan数字..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆