C,C++/JAVA/BASH/ASM ARENA

वह प्रदीप जो दीख रहा है झिलमिल दूर नही है थक कर बैठ गये क्या भाई मन्जिल दूर नही है चिन्गारी बन गयी लहू की बून्द गिरी जो पग से चमक रहे पीछे मुड देखो चरण-चिनह जगमग से बाकी होश तभी तक, जब तक जलता तूर नही है थक कर बैठ गये क्या भाई मन्जिल दूर नही है अपनी हड्डी की मशाल से हृदय चीरते तम का, सारी रात चले तुम दुख झेलते कुलिश का। एक खेय है शेष, किसी विध पार उसे कर जाओ; वह देखो, उस पार चमकता है मन्दिर प्रियतम का। आकर इतना पास फिरे, वह सच्चा शूर नहीं है; थककर बैठ गये क्या भाई! मंज़िल दूर नहीं है। दिशा दीप्त हो उठी प्राप्त कर पुण्य-प्रकाश तुम्हारा, लिखा जा चुका अनल-अक्षरों में इतिहास तुम्हारा। जिस मिट्टी ने लहू पिया, वह फूल खिलाएगी ही, अम्बर पर घन बन छाएगा ही उच्छ्वास तुम्हारा। और अधिक ले जाँच, देवता इतन क्रूर नहीं है। थककर बैठ गये क्या भाई! मंज़िल दूर नहीं है।

Programming Quest June 29, 2010

Filed under: C,C++ Programs — whoami @ 09:02
Tags: , , ,

From YoungProgrammer.com

LCM-GCD based question

Question:

We all interested in mathematics and world are divided in to two parts.
One who are interested in mathematics and other who are afraid of mathematics.
Here is a equation:
( X * N ) % Y = 0

Given two number X & Y you have to find minimum N that satisfies the equation.

Input:

Input consists of two positive integer X & Y . (1<=X,Y<=2000000000)

Output:

You have to output minimum N.

Sample Input:

1 5
6 7

Sample Output:

5
7

Note: You have to give the total of all output for each input. 
For the sample input set, you have to give 
7+5 = 12. So answer is 12.

Download data sets

ANS-562252
my solution
#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>
 
using namespace std;
 
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++) 

typedef long long int  int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;
 

 
template <class T> T sqr (T x) {return x * x;}

int gcd(int a,int b)
{
 if(b==0) return a;
 return gcd(b,a%b);
}

int main()
{
 int X,Y;
 int total=0;
 while(1)
 {
 cin>>X>>Y;
 int hcf=gcd(X,Y);
 int lcm=(X*Y)/hcf;
 total+=(lcm/X);
 cout<<total<<endl;
 }




return 0;
}


Advertisements
 

Programming Quest

Filed under: C,C++ Programs — whoami @ 08:43
Tags: ,
If a number is the only number between a prime number and a square number it is 
beprisque.
Such few numbers are: 2 3 8 10
2 is beprisque, because, 1 is a square and 3 is a prime number
3 is beprisque, because, 2 is prime and 4 is a square number
What's the 100th beprisque number ?

ANS-119026
From Young programmer.com

my solution (not optimized)

#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>
 
using namespace std;
 
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++) 

typedef long long int  int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;
 

 
template <class T> T sqr (T x) {return x * x;}

int main()
{
 static int prime[1000009];
 static int sqr[1000009];
 for(int i=1;i<=1000;i++)
 sqr[i*i]=1;
 
 prime[1]=0;prime[2]=1;

 for(int i=3;i<=1000000;i=i+2)
 {
 int flag=1;
 for(int j=3;j<=sqrt(i);j++)
 {
 if(i%j==0) { flag=0;break;}
 else continue;
 }
 if(flag==1) prime[i]=1;
 }

 int count=0;
 for(int i=2;i<=120000;i++)
 {
 if((sqr[i-1]==1&&prime[i+1]==1)||(sqr[i+1]==1&&prime[i-1]==1)){cout<<i<<endl; ++count;}
 }

 cout<<count<<endl;
 


return 0;
}

here is the few BEPRISQUE number
2
3
8
10
24
48
80
82
168
224
226
360
440
442
728
840
1088
1090
1224
1368
1522
1848
2026
2208
2400
3024
3250
3720
3968
4760
5040
5624
5928
6562
7920
8648
9802
10608
11026
11448
12322
13688
13690
14160
14640
15130
16128
17160
18224
19320
21024
21610
24024
25920
28560
29242
29928
31328
33488
36480
42024
44520
47088
47962
49728
50626
53360
54288
56168
56170
57120
59050
61008
62002
64008
65026
66048
67080
70224
71288
74528
74530
77840
81224
85848
88210
89400
90600
91808
91810
95480
95482
97968
99224
103042
104328
112224
113568
116280
119026
100

 

Programming Quest

Filed under: C,C++ Programs — whoami @ 07:09
Tags: ,

From Young Programmers site

Determine the number of n-bit sequences that contain no adjacent 1’s. For
example, for n = 3 the answer is 5 (sequences 000, 001, 010, 100, 101 are
acceptable while 011, 110, 111 are not).
Find the answer where n = 10

ANS-144

#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>
 
using namespace std;
 
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++) 

typedef long long int  int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;
 

 
template <class T> T sqr (T x) {return x * x;}

int main()
{
 char s[1028];
 int count=0;
 for(int i=0;i<1024;i++)
 {
 int tmp=i;
 int j=0;
 if(i==0) {s[0]='0';s[1]='\0';}
 while(tmp>0)
 {
 int rem=tmp%2;
 s[j++]=rem+'0';
 tmp=tmp/2;
 }
 if(i!=0)
 s[j]='\0';
 //printf("%s\n",s);

 if(!strstr(s,"11")) ++count;   
 }

 cout<<count<<endl;

return 0;
}


 

Programming Question

Filed under: C,C++ Programs — whoami @ 05:57
Tags:

A number has 6 different digits. If the last digit is moved to the front, then a new number is formed which is exaclty 5 times the old number. With which number did we start?

my solution


#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>

using namespace std;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++)

typedef long long int  int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;



template <class T> T sqr (T x) {return x * x;}

int main()
{
 char s[15];
 int count[10];
 for(int i=102345;i<=999999;i++)
 {
 sprintf(s,"%d",i);
 for(int k=0;k<10;k++)
 count[k]=0;
 int flag=1;
 for(int j=0;j<strlen(s);j++){
 count[s[j]-'0']++;
 if(count[s[j]-'0']>=2) {flag=0;break;}
 else continue;
 }
 if(flag==1)
 {
 char str[15];
 str[0]=s[strlen(s)-1];
 for(int j=0;j<strlen(s)-1;j++)
 str[j+1]=s[j];
 str[strlen(s)]='\0';
 int num=atoi(str);
 if(num==5*i) printf("%d\n",i);
 }
 }
return 0;
}

//Answer is 142857


 

Fibonacci Or Not

Filed under: C,C++ Programs — whoami @ 05:13
Tags: ,

The condition to check whether the given number is fibonacci or not. Or say printing numbers which are fibonacci ..

Condition :

N is a Fibonacci number if and only if 5 N2 + 4 or 5 N2 – 4 is a square number.
This was given by I Gessel in 1972. Here is the link
program in C++
</div>
<div>//checking whether the number is fibonacci or not
#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>

using namespace std;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef((x).begin() i=(x).begin();i!=(x).end();i++)

typedef long long int  int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;

template <class T> T sqr (T x) {return x * x;}

int main()
{
 int count=0;
 for(int64 i=0;i<1000000000/*2147483639*/;i++)
 {
 int64 N=i;
 int64 tmp1=5*N*N+4,tmp2=5*N*N-4;//whether the number is fibonacci or not
 ldb t1=(ldb)sqrt((ldb)tmp1);
 ldb t2=(ldb)sqrt((ldb)tmp2);
 int64 tt1=(int64)t1,tt2=(int64)t2;
 if(t1==(ldb)tt1||t2==(ldb)tt2){
 printf("%d %lld\n",++count,i);
 }
 }

return 0;
}</div>
<div></div>
<div>
Here is the few Fibonacci numbers
s.no  fib
1 0
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
11 89
12 144
13 233
14 377
15 610
16 987
17 1597
18 2584
19 4181
20 6765
21 10946
22 17711
23 28657
24 46368
25 75025
26 121393
27 196418
28 317811
29 514229
30 832040
31 1346269
32 2178309
33 3524578
34 5702887
35 9227465
36 14930352
37 24157817
38 39088169
39 63245986
40 102334155
41 165580141
42 267914296
43 433494437
44 701408733

 

UVA Contest from BUBT, Bangladesh

Filed under: C,C++ Programs,UVA — whoami @ 02:30
Tags: ,

LINK

Solved 2 problems A and B during the contest.

Problem A Argentina

#include<cstdlib>

#include <cstdio>

#include <iostream>

#include <vector>

#include <cmath>

#include <algorithm>

#include <string>

#include <set>

#include <map>

#include <ctime>

#include <cstring>

#include <cassert>

#include <iomanip>

#include <complex>

#include <queue>

#include <functional>

using namespace std;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)

#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)

#define pb push_back

#define mp make_pair

#define fs first

#define sc second

#define last(a) int(a.size() - 1)

#define all(a) a.begin(), a.end()

#define seta(a,x) memset (a, x, sizeof (a))

#define I (int)

#define SZ(x) ((int) (x).size())

//#define FE(i,x) for(typedef(x).begin() i=(x).begin();i!=(x).end();i++)

typedef long long int  int64;

typedef unsigned long long int uint64;

typedef long double ldb;

typedef pair <int, int> pii;

typedef vector<int>vi;

typedef vector<string> vs;

template <class T> T sqr (T x) {return x * x;}

int main()

{

  int cases;

  cin>>cases;

  char s[100];

  vector<string>vs;

  int a[11],b[11];

  int c=0;

  while(cases--)

  {

  vs.clear();

  for(int i=0;i<10;i++)

  {

    scanf("%s%d%d",s,&a[i],&b[i]);

    string str=(string)(s);

    vs.pb(str);

  }

  for(int i=0;i<10;i++)

  {

    for(int j=0;j<10;j++)

    {

      if(a[i]<a[j])

      {

        swap(a[i],a[j]);swap(vs[i],vs[j]);swap(b[i],b[j]);

      }

      else if(a[i]==a[j])

      {

        if(b[i]>b[j])

        {

          swap(b[i],b[j]);swap(vs[i],vs[j]);swap(a[i],a[j]);

        }

        else if(b[i]==b[j])

        {

           if(vs[i]>vs[j])

           {

             swap(b[i],b[j]);swap(vs[i],vs[j]);swap(a[i],a[j]);

           }

        }//else

      }//else

    }//for

  }//for

   sort(vs.begin(),vs.begin()+5);

   sort(vs.begin()+5,vs.end());

  printf("Case %d:\n",++c);

  printf("(");

  for(int i=5;i<9;i++)

    cout<<vs[i]<<", ";

  cout<<vs[9]<<")\n";

  printf("(");

  for(int i=0;i<4;i++)

    cout<<vs[i]<<", ";

  cout<<vs[4]<<")\n";

  }

return 0;

}</pre>
<pre>

Problem B Bafana Bafana


#include<cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <ctime>
#include <cstring>
#include <cassert>
#include <iomanip>
#include <complex>
#include <queue>
#include <functional>

using namespace std;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define last(a) int(a.size() - 1)
#define all(a) a.begin(), a.end()
#define seta(a,x) memset (a, x, sizeof (a))
#define I (int)
#define SZ(x) ((int) (x).size())
#define FE(i,x) for(typedef(x).begin() i=(x).begin();i!=(x).end();i++)

typedef long long int  int64;
typedef unsigned long long int uint64;
typedef long double ldb;
typedef pair <int, int> pii;
typedef vector<int>vi;
typedef vector<string>vs;



template <class T> T sqr (T x) {return x * x;}

int main()
{
 int cases;
 cin>>cases;
 int N,K,P;
 int i=0;
 while(cases--)
 {
 cin>>N>>K>>P;
 int tmp=(K+P)%N;
 printf("Case %d: ",++i);
 if(tmp==0) cout<<N<<endl;
 else cout<<tmp<<endl;
 }


return 0;
}

 

UVA Contest from Dinajpur, Bangladesh

Filed under: C,C++ Programs,UVA — whoami @ 02:20
Tags: ,

LINK
I solved a single problem during this UVA contest
F Horror Dash

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

int main()
{
  int cases;
  cin>>cases;
  int i=0;
  while(cases--)
  {
     int n;
     cin>>n;
     cout<<"Case "<<++i<<": ";
     int max=-1;
     while(n--){
      int item;
      cin>>item;
      if(item>max) max=item;
      }
    cout<<max;
    if(cases!=0) printf("\n");
  }


return 0;
}