Exemple #1
0
int main(void)
{
    const long MIN = -10000000L;
    const long MAX = 10000000L;
    long start, stop;
    double answer;

    printf("This program computes the sum of the squares of"
           "integers in a range.\nThe lower bound should not "
           "be less than %ld and\n the upper bound "
           "should not be more then %ld.\nEnter the limits "
           "(0 for both limits to quit):\nlower limit: ", MIN, MAX);
    start = get_long();
    printf("upper limit: ");
    stop = get_long();
    while (start != 0 || stop != 0) {
        if (bad_limits(start, stop, MIN, MAX)) {
            printf("Please try again.\n");
        } else {
            answer = sum_squares(start, stop);
            printf("The sum of the squares of the integers ");
            printf("from %ld to %ld is %g\n", start, stop, answer);
        }
        printf("Enter the limits (0 for both limits to quit):\n");
        printf("lower limit: ");
        start = get_long();
        printf("upper limit: ");
        stop = get_long();
    }
    printf("Done.\n");

    return 0;
}
Exemple #2
0
//A recursive function to calculate the sum
int sum_squares (int n) {
	int valueSum = 0;
	
	if (n > 1) {
		valueSum = (n*n) + sum_squares((n-1));	
		
		return valueSum;
		
	} else if (n == 1) {
		return 1;
	
	}

}
Exemple #3
0
int main (void) {
	int n, returnValue;
	
	printf("Please enter the value n for the sum of squared series: ");
	scanf("%d", &n);
	
	returnValue = sum_squares(n);
	
	printf("The sum of squares from 1 to %d is: %d\n", n, returnValue);
	
	return 0;
	

}
Exemple #4
0
float complex
catanhf(float complex z)
{
	float x, y, ax, ay, rx, ry;

	x = crealf(z);
	y = cimagf(z);
	ax = fabsf(x);
	ay = fabsf(y);

	if (y == 0 && ax <= 1)
		return (CMPLXF(atanhf(x), y));

	if (x == 0)
		return (CMPLXF(x, atanf(y)));

	if (isnan(x) || isnan(y)) {
		if (isinf(x))
			return (CMPLXF(copysignf(0, x), y + y));
		if (isinf(y))
			return (CMPLXF(copysignf(0, x),
			    copysignf(pio2_hi + pio2_lo, y)));
		return (CMPLXF(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
	}

	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
		return (CMPLXF(real_part_reciprocal(x, y),
		    copysignf(pio2_hi + pio2_lo, y)));

	if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
		raise_inexact();
		return (z);
	}

	if (ax == 1 && ay < FLT_EPSILON)
		rx = (m_ln2 - logf(ay)) / 2;
	else
		rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4;

	if (ax == 1)
		ry = atan2f(2, -ay) / 2;
	else if (ay < FLT_EPSILON)
		ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2;
	else
		ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;

	return (CMPLXF(copysignf(rx, x), copysignf(ry, y)));
}
Exemple #5
0
long double complex
catanhl(long double complex z)
{
	long double x, y, ax, ay, rx, ry;

	x = creall(z);
	y = cimagl(z);
	ax = fabsl(x);
	ay = fabsl(y);

	if (y == 0 && ax <= 1)
		return (CMPLXL(atanhl(x), y));

	if (x == 0)
		return (CMPLXL(x, atanl(y)));

	if (isnan(x) || isnan(y)) {
		if (isinf(x))
			return (CMPLXL(copysignl(0, x), y + y));
		if (isinf(y))
			return (CMPLXL(copysignl(0, x),
			    copysignl(pio2_hi + pio2_lo, y)));
		return (CMPLXL(nan_mix(x, y), nan_mix(x, y)));
	}

	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
		return (CMPLXL(real_part_reciprocal(x, y),
		    copysignl(pio2_hi + pio2_lo, y)));

	if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
		raise_inexact();
		return (z);
	}

	if (ax == 1 && ay < LDBL_EPSILON)
		rx = (m_ln2 - logl(ay)) / 2;
	else
		rx = log1pl(4 * ax / sum_squares(ax - 1, ay)) / 4;

	if (ax == 1)
		ry = atan2l(2, -ay) / 2;
	else if (ay < LDBL_EPSILON)
		ry = atan2l(2 * ay, (1 - ax) * (1 + ax)) / 2;
	else
		ry = atan2l(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;

	return (CMPLXL(copysignl(rx, x), copysignl(ry, y)));
}
Exemple #6
0
int main(void)
{
    printf("This program calculates the sum of "
           "all integer squares in a range.\n");
    printf("Please enter the lower and the upper integer of the range:\n"
           "(q to quit):\n");
    int m, n;
    while(scanf("%d %d", &m, &n) == 2)
    {
        printf("The sum of squares for all integers within the "
           "limit %d and %d is %ld\n", m, n, sum_squares(m, n));

        printf("Please enter the lower and the upper integer of the range:\n"
           "(q to quit):\n");
    }
    printf("Done!");
    details();
    return 0;
}
Exemple #7
0
int main()
{
	const int MIN = -1000;
	const int MAX = 1000;
	int start;
	int stop;
	double answer;

	printf("This program computes the sum of squares of "
	       "integers in a range.\nThe lower bound should not "
	       "be less than -1000 and\nthe upper bound should not "
	       "be more than +1000.\nEnter the limits (enter 0 for "
	       "both limts to quit):\nlower limit: ");

	start = get_int();
	printf("upper limit: ");
	stop = get_int();
	while (start != 0 || stop != 0)
	{
		if (bad_limits(start, stop, MIN, MAX))
			printf("Please try again.\n");
		else
		{
			answer = sum_squares(start, stop);
			printf("The sum of the squares of the integers from ");
			printf("from %d to %d is %g\n", start, stop, answer);
		}
		printf("Enter the limits (enter 0 for both limits to quit): \n");
		printf("lower limit: ");
		start = get_int();
		printf("upper limit: ");
		stop = get_int();
	}
	printf("Done.\n");

	return 0;
}
int main( void ) {
    float   *a, *b, c, *partial_c;
    float   *dev_a, *dev_b, *dev_partial_c;

    // allocate memory on the cpu side
    a = (float*)malloc( N*sizeof(float) );
    b = (float*)malloc( N*sizeof(float) );
    partial_c = (float*)malloc( blocksPerGrid*sizeof(float) );

    // allocate the memory on the GPU
    // HANDLE_ERROR( cudaMalloc( (void**)&dev_a,
    //                        N*sizeof(float) ) );
    // HANDLE_ERROR( cudaMalloc( (void**)&dev_b,
    //                        N*sizeof(float) ) );
    // HANDLE_ERROR( cudaMalloc( (void**)&dev_partial_c,
    //                        blocksPerGrid*sizeof(float) ) );

    cudaMalloc( (void**)&dev_a,
                N*sizeof(float) );
    cudaMalloc( (void**)&dev_b,
                N*sizeof(float) );
    cudaMalloc( (void**)&dev_partial_c,
                blocksPerGrid*sizeof(float) );

    // fill in the host memory with data
    for (int i=0; i<N; i++) {
        a[i] = i;
        b[i] = i*2;
    }

    // copy the arrays 'a' and 'b' to the GPU
    // HANDLE_ERROR( cudaMemcpy( dev_a, a, N*sizeof(float),
    //                       cudaMemcpyHostToDevice ) );
    // HANDLE_ERROR( cudaMemcpy( dev_b, b, N*sizeof(float),
    //                       cudaMemcpyHostToDevice ) );

    cudaMemcpy( dev_a, a, N*sizeof(float),
                cudaMemcpyHostToDevice );
    cudaMemcpy( dev_b, b, N*sizeof(float),
                cudaMemcpyHostToDevice );

    // New lines
    __modify_Grid(blocksPerGrid, 1);
    __modify_Block(threadsPerBlock, 1, 1);

    // dot<<<blocksPerGrid,threadsPerBlock>>>( dev_a, dev_b,
    //                                         dev_partial_c );

    // New lines
    __begin_GPU();
    dot( dev_a, dev_b, dev_partial_c );
    __end_GPU();

    // copy the array 'c' back from the GPU to the CPU
    // HANDLE_ERROR( cudaMemcpy( partial_c, dev_partial_c,
    //                       blocksPerGrid*sizeof(float),
    //                        cudaMemcpyDeviceToHost ) );

    cudaMemcpy( partial_c, dev_partial_c,
                blocksPerGrid*sizeof(float),
                cudaMemcpyDeviceToHost );

    // finish up on the CPU side
    c = 0;
    for (int i=0; i<blocksPerGrid; i++) {
        c += partial_c[i];
    }

#define sum_squares(x)  (x*(x+1)*(2*x+1)/6)
    printf( "Does GPU value %.6g = %.6g?\n", c,
            2 * sum_squares( (float)(N - 1) ) );

    // free memory on the gpu side
    // HANDLE_ERROR( cudaFree( dev_a ) );
    // HANDLE_ERROR( cudaFree( dev_b ) );
    // HANDLE_ERROR( cudaFree( dev_partial_c ) );

    cudaFree( dev_a );
    cudaFree( dev_b );
    cudaFree( dev_partial_c );

    // free memory on the cpu side
    free( a );
    free( b );
    free( partial_c );
}
                           count);
    } else {
        const Sk4f min(SK_ScalarNearlyZero);
        const Sk4f max(255);
        const float scale = 255;
        sfx *= scale;
        sfy *= scale;
        sdx *= scale;
        sdy *= scale;
        const Sk4f fx4(sfx, sfx + sdx, sfx + 2*sdx, sfx + 3*sdx);
        const Sk4f fy4(sfy, sfy + sdy, sfy + 2*sdy, sfy + 3*sdy);
        const Sk4f dx4(sdx * 4);
        const Sk4f dy4(sdy * 4);

        Sk4f tmpxy = fx4 * dx4 + fy4 * dy4;
        Sk4f tmpdxdy = sum_squares(dx4, dy4);
        Sk4f R = Sk4f::Max(sum_squares(fx4, fy4), min);
        Sk4f dR = tmpxy + tmpxy + tmpdxdy;
        const Sk4f ddR = tmpdxdy + tmpdxdy;

        for (int i = 0; i < (count >> 2); ++i) {
            Sk4f dist = Sk4f::Min(fast_sqrt(R), max);
            R = Sk4f::Max(R + dR, min);
            dR = dR + ddR;

            uint8_t fi[4];
            SkNx_cast<uint8_t>(dist).store(fi);

            for (int i = 0; i < 4; i++) {
                *dstC++ = cache[toggle + fi[i]];
                toggle = next_dither_toggle(toggle);
Exemple #10
0
int solve(int p) {
  int su = sum_naturals(p);
  int suosq = sum_squares(p);
  return su * su - suosq;
}
Exemple #11
0
/*
 * catanh(z) = log((1+z)/(1-z)) / 2
 *           = log1p(4*x / |z-1|^2) / 4
 *             + I * atan2(2*y, (1-x)*(1+x)-y*y) / 2
 *
 * catanh(z) = z + O(z^3)   as z -> 0
 *
 * catanh(z) = 1/z + sign(y)*I*PI/2 + O(1/z^3)   as z -> infinity
 * The above formula works for the real part as well, because
 * Re(catanh(z)) = x/|z|^2 + O(x/z^4)
 *    as z -> infinity, uniformly in x
 */
double complex
catanh(double complex z)
{
	double x, y, ax, ay, rx, ry;

	x = creal(z);
	y = cimag(z);
	ax = fabs(x);
	ay = fabs(y);

	/* This helps handle many cases. */
	if (y == 0 && ax <= 1)
		return (CMPLX(atanh(x), y));

	/* To ensure the same accuracy as atan(), and to filter out z = 0. */
	if (x == 0)
		return (CMPLX(x, atan(y)));

	if (isnan(x) || isnan(y)) {
		/* catanh(+-Inf + I*NaN) = +-0 + I*NaN */
		if (isinf(x))
			return (CMPLX(copysign(0, x), y + y));
		/* catanh(NaN + I*+-Inf) = sign(NaN)0 + I*+-PI/2 */
		if (isinf(y))
			return (CMPLX(copysign(0, x),
			    copysign(pio2_hi + pio2_lo, y)));
		/*
		 * All other cases involving NaN return NaN + I*NaN.
		 * C99 leaves it optional whether to raise invalid if one of
		 * the arguments is not NaN, so we opt not to raise it.
		 */
		return (CMPLX(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
	}

	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
		return (CMPLX(real_part_reciprocal(x, y),
		    copysign(pio2_hi + pio2_lo, y)));

	if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
		/*
		 * z = 0 was filtered out above.  All other cases must raise
		 * inexact, but this is the only only that needs to do it
		 * explicitly.
		 */
		raise_inexact();
		return (z);
	}

	if (ax == 1 && ay < DBL_EPSILON)
		rx = (m_ln2 - log(ay)) / 2;
	else
		rx = log1p(4 * ax / sum_squares(ax - 1, ay)) / 4;

	if (ax == 1)
		ry = atan2(2, -ay) / 2;
	else if (ay < DBL_EPSILON)
		ry = atan2(2 * ay, (1 - ax) * (1 + ax)) / 2;
	else
		ry = atan2(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;

	return (CMPLX(copysign(rx, x), copysign(ry, y)));
}