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

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

Project Euler Problem 17 – Number letter counts August 8, 2013

Filed under: Project Euler — whoami @ 19:56
Tags: ,

/* Warning: Donot try extracting answer from this and sumbit. This will not help you in Long Run */


/* Project Euler Problem 17 */
/*

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
letters. The use of "and" when writing out numbers is in compliance with British usage.
*/

#include<stdio.h>
#include<string.h>

int main()
{

int i,j;
char str[100];

char str2[][100]={"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred",
"thousand"};
int tot=0;


for(i=1;i<1000;i++)
{
//one digit number 1-9
if(i/10 == 0)
{
tot+=strlen(str2[i-1]);
printf("%s\n",str2[i-1]);

}
//two digit number 10-99
else if(i/100 == 0)
{
if(i>=10 && i<=19)
{
tot+=strlen(str2[i-1]);
printf("%s\n",str2[i-1]);
}
else if( i%10 == 0 )
{
tot+=strlen(str2[18+i/10-1]);
printf("%s\n",str2[18+i/10-1]);
}
else
{
int unit=i%10;
int tens=(i/10)%10;
tot=tot+strlen(str2[unit-1]) + strlen(str2[18+tens-1]);
printf("%s-%s\n",str2[18+tens-1], str2[unit-1]);
}

}
//three digit number 100-999
else if(i/1000 == 0)
{
tot+=strlen("hundred");
if(i%100 == 0)
{
tot= tot + strlen(str2[i/100-1]);
printf("%s hundred\n",str2[i/100-1] );
}
else if(i%10 == 0)
{
int hundredth=i/100;
int tens=((i/10)%10);
if(tens == 1)
{
tot=tot+strlen(str2[hundredth-1]) + strlen("ten")+strlen("and");
printf("%s hundred and ten\n",str2[hundredth-1]);

}
else
{
tot=tot+strlen(str2[hundredth-1])+strlen(str2[18+tens-1])+strlen("and");
printf("%s hundred and %s\n",str2[hundredth-1],str2[18+tens-1]);
}
}
else
{
int hundredth=i/100;
int tens=(i/10)%10;
int units=i%10;

tot=tot+strlen(str2[hundredth-1])+strlen("and");
printf("%s hundred and ", str2[hundredth-1]);
if(tens == 1)
{
tot=tot+strlen(str2[9+units]);
printf("%s\n",str2[9+units]);
}
else if(tens == 0)
{
tot=tot+strlen(str2[units-1]);
printf("%s\n",str2[units-1]);
}
else
{
tot=tot+strlen(str2[18+tens-1]) + strlen(str2[units-1]);
printf("%s-%s\n",str2[18+tens-1], str2[units-1]);
}
}
}
//sleep(1);
}

tot=tot+strlen("onethousand");
printf("one thousand\n");
printf("%d",tot);
return 0;
}

Advertisements
 

Young programmer – Inspirational Video June 30, 2013

Filed under: Uncategorized — whoami @ 09:28
Tags: , , , ,

Source – Code.org

 

Project Euler Problem 57 – Square root convergents March 22, 2013

Filed under: Project Euler — whoami @ 11:33
Tags: , , , ,

It is possible to show that the square root of two can be expressed as an infinite continued fraction.

√ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + … ))) = 1.414213…

By expanding this for the first four iterations, we get:

1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666…
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379…

The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.

In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?

Euler57

 

Project Euler Problem 204 Generalised Hamming Numbers March 21, 2013

Filed under: Project Euler — whoami @ 08:46
Tags: , , , ,

A Hamming number is a positive number which has no prime factor larger than 5.
So the first few Hamming numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15.
There are 1105 Hamming numbers not exceeding 108.

We will call a positive number a generalised Hamming number of type n, if it has no prime factor larger than n.
Hence the Hamming numbers are the generalised Hamming numbers of type 5.

How many generalised Hamming numbers of type 100 are there which don’t exceed 10^9?

Hint: Generate prime number less than 100. Check for all the number in range 1-10^9, whether all prime number <100 divides them totally to get final remaining number as 1. Is this happens for particular number then add it to your list otherwise don’t add the number to the list.

euler204

 

My misconception about Compound assignment operators in C,C++ March 18, 2013

Filed under: Uncategorized — whoami @ 09:41
Tags: , , ,

Today i got cleared of a very big misconception about Compound assignment operators.

I had wrongly understood statements like

A -=b+c as A=A-b+cBut correct is A -=b+c is same as A = A – (b+c)

More explanation can be find here

I have built this wrong concept because in elementary C books there was rarely mentioning of three variables for compound assignment operator examples. Like

The last example(bottom) below i never came through 😦

expression

evaluation

value += increase;

value = value + increase;

a -= 5;

a = a - 5;

a /= b;

a = a / b;

price *= units + 1;

price = price * (units + 1)

 

Check your pointer skills. January 4, 2013

Filed under: Uncategorized — whoami @ 11:47
Tags: , , , ,

Good Evening !!!

Explain the following :-

1. int (*(*foo)(void ))[3]

2.char *const argv[]

3. char * const *argv

4. void (*p[10]) (void (*)() )

Things that you might know well:-

1. const char*

2. char* const

3. const char* const

4. const *char ->tricky

5. char const * -> similar as one of the above(guess which one?)

🙂

 

Minimum Scalar Product May 21, 2010

Filed under: C,C++ Programs,Google Code Jam — whoami @ 20:31
Tags: , ,

GOOGLE CODE JAM 2008
Round 1A 2008

AC for both Smaller and Bigger Data

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>

using namespace std;
int main()
{
  FILE *p=fopen("a.txt","w");
  FILE *in=fopen("A-large-practice.in","r");
  long long int t,n,i,j,k;
  long long int x[10000];
  long long int y[10000];
  long long int total;
  long long int c=0;
  fscanf(in,"%lld",&t);
  while(t--){
    fscanf(in,"%lld",&n);
    for(i=0;i<n;i++)
      fscanf(in,"%lld",&x[i]);
    for(i=0;i<n;i++)
      fscanf(in,"%lld",&y[i]);

    sort(x,x+n);
    sort(y,y+n);
    total=0;
    for(i=0,j=n-1;i<n;i++,j--)
      total+=(long long int )(x[i]*y[j]);
    fprintf(p,"Case #%lld: %lld\n",++c,total);
   }
  fclose(p);
  fclose(in);
return 0;
}