Sunday, October 1, 2017

543 - Goldbach's Conjecture

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back

int num = 1500005;
bool check[1500005];

void sieve ()
{
    int i,j;

    check[0] = check[1] = true;

    for (i=4; i<=num; i+=2)
        check[i] = true;

    for (i=3; i*i<=num; i+=2)
        if (!check[i])
            for (j=i*i; j<=num; j+=2*i)
                check[j] = true;

    return;
}


int main (void)
{
    int n,i,h;
    bool k;

    sieve ();

    while (cin >> n && n)
    {
        h = n/2;
        k = false;

        for (i=2; i<=h; i++)
        {
            if (!check[i] && !check[n-i])
            {
                k = true;
                pf ("%d = %d + %d\n",n,i,n-i);
                break;
            }
        }

        if (k == false)
            pf ("Goldbach's conjecture is wrong.\n");
    }

    return 0;
}

495 - Fibonacci Freeze

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

import java.util.Scanner;
import java.math.BigInteger;

public class Main
{
public static void main (String[] args)
{
Scanner in = new Scanner (System.in);

int n,i;

while (in.hasNext())
{
n = in.nextInt();

BigInteger a = BigInteger.valueOf(1),b=a,sum;

if (n == 0)
System.out.println ("The Fibonacci number for 0 is 0");
else
{
for (i=1; i<=n; i++)
{
if (i == n)
System.out.println ("The Fibonacci number for " +n+ " is " +a);

sum = a.add(b);
a = b;
b = sum;
}
}
}
}
}

483 - Word Scramble

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int

int main (void)
{
    /*freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);*/
    int len,l,i,j;
    string str,a;
    bool k;
    char ch;

    while (getline(cin,str))
    {
        len = str.size();

        for (i=0; i<len; i++)
        {
            if (i == len-1)
            {
                a.pb(str[i]);
                reverse (a.begin(),a.end());
                cout << a;
                a.clear();
            }
            else
            {
                if (str[i] == ' ')
                {
                    reverse (a.begin(),a.end());
                    cout << a;
                    a.clear();
                    cout << str[i];
                }
                else
                    a.pb(str[i]);
            }
        }

        cout << endl;
    }

    return 0;
}

444 - Encoder and Decoder

/***

            Bismillahir Rahmanir Rahim

            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/


#include <bits/stdc++.h>

using namespace std;

#define sf scanf

#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert
#define llu unsigned long long
#define lld long long
#define U unsigned int

char strinttochar (string str)

{
    int len=str.size(),i,val=0,k=0;

    for (i=len-1; i>=0; i--)

        val += (str[i]-'0') * pow(10.0,k++);

    return char(val);

}

int main (void)

{
    /*freopen ("input.txt","r",stdin);
    freopen ("output.txt","w",stdout);*/
    string str,a;
    int len,i,val;

    while (getline(cin,str))

    {
        len = str.size();

        if (isalpha(str[0]))

        {
            reverse (str.begin(),str.end());

            for (i=0; i<len; i++)

            {
                val = str[i];
                a = to_string(val);
                reverse (a.begin(),a.end());
                cout << a;
            }

            a.clear();

        }
        else
        {
            reverse (str.begin(),str.end());

            for (i=0; i<len; i++)

            {
                if (str[i] == '1')
                {
                    a.pb(str[i]);
                    a.pb(str[i+1]);
                    a.pb(str[i+2]);
                    cout << strinttochar(a);
                    i+=2;
                    a.clear();
                }
                else
                {
                    a.pb(str[i]);
                    a.pb(str[i+1]);
                    cout << strinttochar(a);
                    i++;
                    a.clear();
                }
            }
        }

        cout << endl;

    }

    return 0;

}

371 - Ackermann Functions

/***

            Bismillahir Rahmanir Rahim
            Read the name of Allah, who created you!!!
            Author : Shah Newaj Rabbi Shishir
            Department of CSE, City University, Bangladesh.

***/

#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define scase sf ("%d",&tc)
#define sn sf ("%d",&n)
#define whilecase while (tc--)
#define eof while (cin >> n)
#define forloop for (pos=1; pos<=tc; pos++)
#define arrayloop (i=0; i<n; i++)
#define cinstr cin >> str
#define getstr getline (cin,str)
#define pcase pf ("Case %d: ",pos)
#define pb push_back
#define in insert

unsigned int func (unsigned int n,unsigned int count)
{
    if (n & 1)
        n = 3*n+1;
    else
        n >>= 1;

    if (n == 1)
        return ++count;

    func (n,++count);
}

int main (void)
{
    unsigned int a,b,i,p,k,num,maxi;

    while (sf ("%u %u",&a,&b) && a && b)
    {
        if (a > b)
            swap (a,b);

        maxi = num = 0;

        for (i=a; i<=b; i++)
        {
            k = func (i,0);

            if (k > maxi)
            {
                maxi = k;
                num = i;
            }
        }

        pf ("Between %u and %u, %u generates the longest sequence of %u values.\n",a,b,num,maxi);
    }

    return 0;
}