我想弄清楚一種方式,以更快和更優化的方式解決這個特定的問題。我不知道是否有可能讓這些代碼在多個內核和線程上運行,或者如何以某種方式將其卸載到GPU,但速度越快,計算得越好。是否有更優化的方式在java中進行簡單的計算?
public class numberCounter
{
public static void main(String[] args)
{
//Used only to check speed of calculation
long start = System.currentTimeMillis();
//Initialized variables
double x, y, z;
//30 can be substituted out but i used it as a test.
//I am unable to calculate 31 and above.
long t = (long) Math.pow(2, 30);
//used later for counting
long counter = 0;
//starting number
System.out.println("Number - " + t);
for(int i = 1; i < t; i++)
{
x = i % 2;
y = i % 3;
z = i % 5;
if(x*y*z > 0)
counter++;
}
System.out.println();
//prints out number of numbers that follow the rule above
System.out.printf("Numbers: %.0f\n", counter);
long end = System.currentTimeMillis();
//prints out time taken
System.out.println((end - start) + " ms");
}
}
這看起來很像一個家庭作業問題,所以我會提供一些提示。提示#1:你應該能夠在一段時間內做到這一點。提示#2:當t = 30時櫃檯的價值是多少?當t = 60時? – user3486184
這在技術上是我給自己的一個家庭作業問題,試圖證明一個數學問題,而我只用時間來看看需要多長時間來查看優化是否會使其更快。噸在30 = 286331153.我無法計算以上。 –