#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
#define PMAX 1000010
bool ver[PMAX];
vector<int> plist;
int sieve(int lim) {
int i, j, k;
plist.clear();
plist.push_back(2);
for (int i=3 ; i<=lim ; i+=2) {
if (ver[i]==false) {
plist.push_back(i);
for (int j=3 ; i*j<=lim ; j+=2) {
ver[i*j] = true;
}
}
}
}
int main() {
sieve(PMAX);
int n;
while (scanf("%d",&n)==1) {
if (n == 0) break; // Program terminated
int a=-1, b=-1;
bool found = false;
for (int i=0 ; i<plist.size() && plist[i]<=n ; i++) {
a = n-plist[i];
b = plist[i];
if (a>1 && b>1 && (a&1) && (b&1) && ver[a] == false && ver[b] == false) {
found = true;
break;
}
}
if (found == true) {
printf("%d = %d + %d\n",n,b,a);
} else {
printf("Goldbach's conjecture is wrong.\n");
}
}
return 0;
}
Showing posts with label Sieve. Show all posts
Showing posts with label Sieve. Show all posts
Wednesday, March 19, 2014
[UVa] 543 - Goldbache's Conjecture
Thursday, November 10, 2011
[UVa] 10948 - The Primary Problem
#include <cstdio>
#include <iostream>
using namespace std;
bool ver[10000500];
int sieve()
{
int i, j, k=0;
//lst[k++]=2;
for (i=3 ; i<=10000000 ; i+=2)
{
if (!ver[i])
{
//lst[k++]=i;
for (j=3 ; i*j<=10000000 ; j+=2)
{
ver[i*j]=true;
}
}
}
//lst[0]=2;
return k;
}
int main()
{
sieve();
int n, i;
bool found;
while (std::cin >> n && n)
{
std::cout << n << ":" << std::endl;
if ((((n-2)&1) && !ver[n-2]) || (n-2 == 2))
{
std::cout << "2+" << n-2 << std::endl;
continue;
}
found = false;
for (i=3 ; i<=((n+1)/2) ; i+=2)
{
if (!ver[i] && !ver[n-i] && (n-i)&1)
{
std::cout << i << "+" << n-i << std::endl;
found = true;
break;
}
}
if (!found) std::cout << "NO WAY!" << std::endl;
}
return 0;
}
Wednesday, October 26, 2011
[UVa] 160 - Factors & Factorials
#include <cstdio>
#include <iostream>
#define MP 100
using namespace std;
bool ver[300]={false};
int list[1000];
int siever() {
int i, j, k=0;
list[k++]=2;
for (i=3 ; i<=200 ; i+=2)
{
if (ver[i]==false)
{
list[k++]=i;
for (j=3 ; i*j<=200 ; j+=2)
{
ver[i*j]=true;
}
}
}
list[0]=2;
return k;
}
int test(int x, int p)
{
int i, counter=0, temp, rem=1, n=x;
for (i=x ; i>1 ; i--)
{
n=i;
while (n%p == 0)
{
counter++;
n/=p;
}
}
return counter;
}
int main() {
int pl=siever();
int i, n;
while (cin >> n && n)
{
printf("%3d! =",n);
for (i=0 ; i<pl && list[i]<=n ; i++)
{
printf("%3d",test(n,list[i]));
if (i%14 == 0 && i>0 && list[i+1]<=n) printf("\n%6c",' ');
}
printf("\n");
}
return 0;
}
Subscribe to:
Posts (Atom)
Connect Rapoo MT750S with Linux (Tested on Manjaro)
I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...
-
I like coding a lot, keeps me glued to the PC for hours. For that reason it's a need to edit the Syntax Highlighter to suit my eyes for...
-
Method: The problem at first glance seems too straightforward but it's not that much. Think a bit about the lines "Erin can add ...
-
I've been constantly switching between my two OSs, Win7 and Linux Mint for just flashing a ROM to my Xperia device. And ofcourse, I hat...