10311 - Goldbach and Euler

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

<:3)~~ wrote:why isnt any one replying!!!!!! :cry:
One reason could be -- you haven't specified the problem number !!

Please specify the problem number & name properly -- and don't create a new thread if one already exists.

I used both sieve and Miller to get ACed.
<:3)~~
New poster
Posts: 16
Joined: Wed Dec 06, 2006 6:57 pm

Post by <:3)~~ »

OK i will keep these things in mind ..But please tell me why am i getting signal 11(undefined memry refrence).
code:
*DELETED*After the 29th submission i got AC
this sux.
anyways thx again JAN.
i dont know anything about this bit sieve..i just pasted it from this link
*deleted*
Plzz help!!
kn
New poster
Posts: 28
Joined: Fri Apr 13, 2007 8:53 am

Post by kn »

HI.....
I used bitwise sieve, and it seems that my program can sieve all those prime numbers....

I wonder why the following piece of code receives a WA...

Code: Select all

AC!! THX VERY MUCH!
THX FOR THE CORRECT I/O!
Last edited by kn on Fri May 18, 2007 11:55 pm, edited 2 times in total.
mmonish
Experienced poster
Posts: 109
Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST

Post by mmonish »

Try this case

input
10

output
10 is the sum of 3 and 7.

the difference between the two primes must be positive.
kn
New poster
Posts: 28
Joined: Fri Apr 13, 2007 8:53 am

Post by kn »

mmonish wrote:Try this case

input
10

output
10 is the sum of 3 and 7.

the difference between the two primes must be positive.
After knowing that I had this misconception (not knowing that the diff must be +ve =.=)
And wrongly thought that 1 is considered as a prime number in this question...
Finally...AC!!
THX VERY VERY VERY MUCH!!
Sebastien
New poster
Posts: 1
Joined: Wed Oct 27, 2010 11:43 am

Re: 10311 - Goldbach and Euler

Post by Sebastien »

Hi all,

I have read the forum and checked for all the pitfalls.

1 is not prime for me
p1 < p2
26 is the sum of 7 and 19. (not 13 & 13)

& I have checked it against the test set here,
http://online-judge.uva.es/board/viewto ... 20&t=10600
posted by Caesum.
& it works fine.

But I am still getting WA. Can you tell me where I am failing?

Code: Select all

#include <iostream>
#define HM 100000000

using namespace std;

bool prime_mark [HM];

//sieve the primes with the "Eratosthenes Prime Sieve" 
//until 100'000'000 and not until the square root.
void prime_sieve (int n) {
  
  int s = n;
  int num_primes = 1;
  
  for (int i=2; i < n; i+=2 ) {
    prime_mark[i] = false;
    prime_mark[i+1] = true;
  }
  
  prime_mark[0] = prime_mark[1] = false;
  prime_mark[2] = true;
  
  for( int i=3; i<s; i+=2 ){
    if (prime_mark[i]) {
      num_primes++;
      for (int j=3*i; j < n; j+=(2*i)){
	prime_mark[j] = false;
      }
    }
  }
  
}

bool isEven (int n){
  if (n%2 == 0){
    return true;
  }
  else { 
    return false;
  }
}

int main() {
  
  prime_sieve (HM);
  
  int n;
  int p1 = -1;
  int p2 = -1;
  
  while (!cin.eof()){
    cin >> n;
    
    // if input number is odd: there is only the possibility to form an odd number with an even + an odd number. 
    // The only even prime is 2. so for this case, test if n-2 is a prime.
    if (!(isEven(n))){
      if (prime_mark[n-2]){
	cout << n << " is the sum of 2 and " << n-2 << "." << endl;
      }
      else {
	cout << n << " is not the sum of two primes!" << endl;
      }
    }
    
    // if input number is even: it must be a sum of two odd numbers, since there are no two distinct even primes
    
    /*
    1. divide n by 2, save to half
    2. if half is even, set p1 to half-1 and p2 to half+1;
    else (if half is odd) set p1 to half-2 and p2 to half+2.
    3. check if p1 and p2 are primes (if yes, you have found a minimized sum for your n and you can break)
    4. else p1 -= 2 and p2 +=2 and check again if both are primes and so forth...
    */
    
    else{
      //1. divide n by 2, save to half
      int halfn = n/2;
      
      //2. if half is even, set p1 to half-1 and p2 to half+1;      
      if ((isEven(halfn))) {
	p1 = halfn-1;
	p2 = halfn + 1;
	if ( prime_mark[p1] && prime_mark[p2]){
	  
	}
      }
      // else (if half is odd) set p1 to half-2 and p2 to half+2.
      else {
	p1 = halfn - 2;
	p2 = halfn + 2;
      }      
      
      //3. check if p1 and p2 are primes (if yes, you have found a minimized sum for your n and you can break)
      //4. else p1 -= 2 and p2 +=2 and check again if both are primes and so forth...
      while (!prime_mark[p1] || !prime_mark[p2]){
	if ( prime_mark[p1] && prime_mark[p2]){	  
	  break;	  
	}
	else{
	  p1 = p1 -2;
	  p2 = p2 + 2;
	}
      }
      
      // Make sure you are not going minus
      // When you did not have this check, you were getting:
      /*
      6 is the sum of -7 and 13.
      4 is the sum of -7 and 11.
      2 is the sum of -9 and 11.
      */
      if ( p1>0 && p2>0) {
	cout << n << " is the sum of " << p1 << " and " << p2 << "." << endl;
      }
      else {
	cout << n << " is not the sum of two primes!" << endl;
      }
      
    }
    
  }    
  
  return 0;
}

Thanks a lot :) ,
Sebastien
Shafaet_du
Experienced poster
Posts: 147
Joined: Mon Jun 07, 2010 11:43 am
Location: University Of Dhaka,Bangladesh
Contact:

Re: 10311 - Goldbach and Euler

Post by Shafaet_du »

You can store prime upto 10^8 and check primality in O(1). Use bitwise sieve to save memory. If the number is even find the largest prime just smaller than n/2 and than iterate to 0. I found it using binary search. Still my runtime is not great,1.332 sec(time limit 10sec). If the number is odd,just check if n-2 is prime. and remember the condition abs(p1-p2)>0.
novice2
New poster
Posts: 3
Joined: Tue May 08, 2012 6:30 pm

Re: 10311 - Goldbach and Euler

Post by novice2 »

bool isPrime(int n)
{
// n>2 and odd
if(n==3)return true;
if( n%3==0)return false;
int j=1,k=5;
for(;k*k<=n;j++,k=6*j-1)
{ if(n%k==0)return false;
if(n%(k+2)==0)return false;
}
return true;
}
// no sieve no miller rabin
// just did in Ad-hoc way with this isPrime funtion to get accepted in 3 seconds
novice2
New poster
Posts: 3
Joined: Tue May 08, 2012 6:30 pm

Re: 10311 - Goldbach and Euler

Post by novice2 »

but using bitwise sieve reduces runtime to 0.6 seconds
Post Reply

Return to “Volume 103 (10300-10399)”