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;
}
#include
This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.
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 lex program to delete space from the program
write an assembly language program to find sum of N numbers
1. Design an algorithm to compute sum of the squares of n 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
fdsgfhgdfhgdf
How to write a program for mouse in microprocessor?
program to find maximum of two numbers using pointers
write it in 8085
To write a C++ program to display the student details using class and array of object.