answersLogoWhite

0


Best Answer

It is not possible to print the sum of alternate prime numbers because prime numbers are infinite and, therefore, alternate prime numbers are also infinite. Thus the only way you can write such a program is if you set an upper limit on the largest prime you wish to consider. That limit will be determined by the maximum sum you can store in a built-in integral type which is ULLONG_MAX. In other words, you must stop generating prime numbers if the sum would overflow.

We achieve this by storing ULLONG_MAX and then decrement by each alternate prime until the next prime is greater than the remainder. The largest sum is therefore ULLONG_MAX minus that remainder. The following program demonstrates this.

#include<io.sys>

#include<limits.h>

#include<math.h>

// Forward declarations of required functions (see definitions below)

bool is_prime (unsigned long long);

unsigned long long next_prime (unsigned long long);

int main (void) {

unsigned long long remainder = ULLONG_MAX;

unsigned long long prime = 2; // 2 is the first prime (same as next_prime (0) or next_prime (1))

unsigned long long largest = prime; // keep track of largest prime in sum

while (prime<=remainder) { // test for overflow!

remainder -= prime; // subtract the prime

largest = prime; // update largest sum

prime = next_prime (next_prime (prime)); // skip the next prime and get the next

}

printf ("The sum of alternate primes is:\n%u", ULLONG_MAX - remainder);

printf ("The largest prime in the sum is: %u\n", largest);

return 0;

}

// Returns true if num is prime, otherwise false

bool is_prime (unsigned long long num) {

if (num<2) return false; // 2 is the first prime

if (!(num%2) return num==2; // 2 is the only even prime

unsigned long long max_factor = (unsigned long long) sqrt (num);

for (unsigned long long factor=3; factor<=max_factor; factor+=2) // test all odd factors >= 3

if (!(num%factor)) return false; // factor is prime factor of num, so num is non-prime

return true; // num has no prime factors, so num is prime

}

// Returns the next prime greater than num.

unsigned long long next_prime (unsigned long long num) {

while (!is_prime (++num));

return num;

}

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a easy program to print the sum of squares of alternative prime numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a C Program to print sum of squares of odd numbers?

#include


Write a program to print first 100 alternative prime numbers?

This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.


Can you write every integer as the sum of two nonzero perfect squares?

No.First of all, you can't write negative numbers as sums of perfect squares at all - since all perfect squares are positive.Second, for natural numbers (1, 2, 3...) you may need up to 4 perfect squares: http://en.wikipedia.org/wiki/Lagrange's_four-square_theoremNo.First of all, you can't write negative numbers as sums of perfect squares at all - since all perfect squares are positive.Second, for natural numbers (1, 2, 3...) you may need up to 4 perfect squares: http://en.wikipedia.org/wiki/Lagrange's_four-square_theoremNo.First of all, you can't write negative numbers as sums of perfect squares at all - since all perfect squares are positive.Second, for natural numbers (1, 2, 3...) you may need up to 4 perfect squares: http://en.wikipedia.org/wiki/Lagrange's_four-square_theoremNo.First of all, you can't write negative numbers as sums of perfect squares at all - since all perfect squares are positive.Second, for natural numbers (1, 2, 3...) you may need up to 4 perfect squares: http://en.wikipedia.org/wiki/Lagrange's_four-square_theorem


Write a program in Lex to eliminate white space and collect numbers as a token?

write a lex program to delete space from the program


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


Write algorithm compute sum of square of N numbers?

1. Design an algorithm to compute sum of the squares of n numbers?


Shell program for gcd of three given numbers?

write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? check bellow link http://bashscript.blogspot.com/2009/08/gcd-of-more-than-two-numbers.html


How do you write a program in objective c numbers 1-100 prime numbers?

fdsgfhgdfhgdf


Could you Write a program for 8086 microprocessor that displays on the monitor the average of 2 numbers from an array?

How to write a program for mouse in microprocessor?


How to write a C program to find largest 2 numbers using pointers?

program to find maximum of two numbers using pointers


Write a program to add two 8 bit numbers in microprocessor 8051?

write it in 8085


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.