Showing posts with label Number Theory. Show all posts
Showing posts with label Number Theory. Show all posts

Wednesday, March 19, 2014

[UVa] 543 - Goldbache's Conjecture

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

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

[UVa] 1230 - MODEX

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;

ll sqr(ll v)
{
    return v*v;
}

ll bigMod(ll p, ll v, ll m)
{
    if (p==0) return 1;
    else if (p&1) return (v%m * bigMod(p-1,v,m))%m;
    else return (sqr(bigMod(p/2,v,m)))%m;
}

int main()
{
    int tst;
    ll x, y, n;

    cin >> tst;

    while (tst--)
    {
        cin >> x >> y >> n;
        cout << bigMod(y,x,n) << endl;
    }

    cin >> tst;

    return 0;
}

[UVa] 568 - Just the Facts

#include <cstdio>
#include <iostream>
using namespace std;

int fact[20000];

int main()
{
    int i, f, idx;
    fact[0] = fact[1] = 1;
    f = 1;

    for (i=2 ; i<=10000 ; i++)
    {
        f*=i;
        while (f%10 == 0)
            f/=10;

        f = f%100000;
        fact[i] = f%10;
    }

    while (scanf("%d",&idx)==1)
    {
        printf("%5d -> %d\n",idx,fact[idx]);
    }

    return 0;
}

Monday, October 31, 2011

[UVa] 10830 - A New Function

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

typedef unsigned long long ull;
typedef long long xll;
typedef long double ld;

using namespace std;
ull sumfind(ull a, ull n)
{
    return ((n*(n+1)/2) - (a*(a-1)/2));
}
ull csod(ull n)
{
    ull root = (ull)sqrt((double)n), i, j;
    ull lim, sum=0;

    for (i=2 ; i<=root ; i++)
    {
        lim = n/i;
        sum += (sumfind(i+1,lim) + (i*(lim-i+1)));
    }

    return sum;
}

int main()
{
    ull n, sum=0, kase=1;

    while (cin >> n && n)
    {
        cout << "Case " << kase++ << ": " << csod(n) << 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...