在C(1秒以内)中是否有任何快速方法可以找到两个数字之间的完美平方数.对于前者 对于1 < - > 10,我们有2个完美的正方形4和9.但是在1 < - > 2 ^ 60或其他更大的数字之间呢.
这很慢
while(i*i<=n)
{
sum+=i==((long long)(sqrt(i*i)));
i++;
}
Run Code Online (Sandbox Code Playgroud)
其中n是2 ^ 60,我们从i = 2开始.
有人可以帮助我改进这些代码并给我一些提示.我试图自己创建一个OpenMP版本的Mandelbrot.我是OpenMP初学者,在这里我没有加快速度,这可能是因为#pragma omp critical
我现在想不出更好的主意.
int main()
{
// picture resolution
int iX,iY;
const int ImageWidth = 1000;
const int ImageHeight = 1000;
double Cx,Cy;
const double CxMin=-2.5;
const double CxMax=1.5;
const double CyMin=-2.0;
const double CyMax=2.0;
double PixelWidth=(CxMax-CxMin)/ImageWidth; /* scaled x coordinate of pixel (must be scaled to lie somewhere in the Mandelbrot
X scale (-2.5, 1.5) */
double PixelHeight=(CyMax-CyMin)/ImageHeight;/* scaled y coordinate of pixel (must be scaled to lie somewhere in the Mandelbrot
Y scale (-2.0, 2.0) */ …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Insertion排序编写OpenMP解决方案,但是我遇到问题要让它并行运行并给出正确的结果:).有没有办法让Insertion排序并行运行.
这是我的代码:
void insertionsort(int *A, int num)
{
// clock_t start, stop;
//
// start=clock();
int k;
#pragma omp parallel for shared(A) private(k)
for(int n = 1; n < num; n++)
{
int key = A[n];
k = n;
#pragma omp critical
for(;k>0 && A[k-1]> key;k--)
{
A[k] = A[k-1];
}
A[k] = key;
}
// stop=clock();
// cas = (double)(stop-start)/CLOCKS_PER_SEC;
}
Run Code Online (Sandbox Code Playgroud)