Showing posts with label Prime Numbers. Show all posts
Showing posts with label Prime Numbers. Show all posts

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;
}

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...