问题
Lets see we want to find all numbers between 1 to 1000 which are represented as a sum of two prime numbers. e.g 8 = 3+5, 24 = 13+11
Now this can be done in O(n^2) by iterating through the list of prime numbers between 1 to 1000.
Is there anyway of doing the same thing in less than O(n^2).Is there a method for doing this in linear time ?
回答1:
Make an array p
of 1000 booleans. Set p[i]
to true
if i
is prime, and false
otherwise.
Then the O(N^2)
algorithm becomes easy: go through numbers k
1 through 1000 in the outer loop, then go through all primes x
greater than k
in an inner loop, and check if there exists a prime such that p[k-x]
is true
:
for k in 1..1000
for x in primes greater than k
if (p[x-k])
print k can be represented as x plus (x-k)
break
I doubt that the check could be performed in constant time for a total running time of O(N)
for the 1000 numbers, because computer-aided verification currently proceeds at a rather slow speeds.
回答2:
For all the even numbers we know now, they can be represented as the sum of 2 prime numbers(see Goldbach's conjecture)
For all the odd numbers, if it can be represented as the sum of 2 prime numbers, one of the them must be 2, and the other should be an odd prime.
So the total number should be (1000/2 - 1) + (prime number count from 3 to 997),
in which,
(1000/2 - 1) is the total number of series 4, 6, 8, 10...
(prime number count from 3 to 997) is the total number of series 5(2+3), 7(2+5), 9(2+7), 13(2+11) ...
回答3:
1 ! 1 is a good formula for this. first do, 1000 + 1 divided by 5x + 38. this is according to the ATF Theorem. try this and you will get the answer.
回答4:
import java.util.Scanner;
public class SumOfNPrmNos_2 {
public static void main(String[] args) {
int swap,sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number Range From ");
int small=sc.nextInt();
System.out.println("Enter Number Range To ");
int big=sc.nextInt();
for (int i =small+1 ; i<big; i++) {
char check='T';
for (int j=2 ; j<=Math.sqrt(i) ; j++){
if(i%j==0){
check='F';
break;
}
}
if(check=='T'){
sum=sum+i;
}
}
System.out.println("Sum Is : = "+sum);
}
}
回答5:
Explanation: here I made a user-defined function for checking prime of the input number. This number is divided into two parts first is num-1 and second is 1(their sum is equal to num), now the first one is decremented and second is incremented until they both become equal or greater, for each of these numbers, I find if both of them are prime, if both are prime , then break is used to come out of the loop and print those numbers.
#include<stdio.h>
int prime(int);
int main()
{
int num, r1, r2, i, j, c=0;
printf("enter number\n");
scanf("%d", &num);
for(i=num-1, j=1; i>=j; i--, j++) //this divides numb
{
r1=prime(i);
r2=prime(j);
if(r1==1 && r2==1)
{
printf("Numbers are %d and %d\n", i, j);
c++;
}
}
if(c==0)
printf("cannot be expressed as sum of primes\n");
}
int prime(int n)
{
int i;
for(i=2; i<=n; i++)
if(n%i==0)
break;
if(n==i)
return 1;
else
return 0;
}
来源:https://stackoverflow.com/questions/14720904/how-to-find-a-number-as-a-sum-of-prime-numbers