检查长整数是否为多维数据集的快速方法(在Java中) [英] Fast way to check if long integer is a cube (in Java)

查看:94
本文介绍了检查长整数是否为多维数据集的快速方法(在Java中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,在该程序中,我需要检查某些大数(多维数据集的排列)是否是立方的(对于某些n等于n ^ 3).

I am writing a program in which I am required to check if certain large numbers (permutations of cubes) are cubic (equal to n^3 for some n).

此刻我只是使用方法

static boolean isCube(long input) {
    double cubeRoot = Math.pow(input,1.0/3.0);
    return Math.round(cubeRoot) == cubeRoot;
}

,但是在处理大量数字(10位数以上)时,这非常慢.有没有一种更快的方法来确定整数是否为立方体?

but this is very slow when working with large numbers (10+ digits). Is there a faster way to determine if integer numbers are cubes?

推荐答案

只有2 ^ 21个多维数据集不会长时间溢出(如果允许负数,则溢出2 ^ 22-1),因此您可以使用一个HashSet查找.

There are only 2^21 cubes that don't overflow a long (2^22 - 1 if you allow negative numbers), so you could just use a HashSet lookup.

这篇关于检查长整数是否为多维数据集的快速方法(在Java中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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