Thursday, 12 February 2015

Concrete Mathematics Exercise Solution: Chapter 1[problem no: 13].

Question: 1.13: What’s the maximum number of regions definable by zig-zag lines, 

each of which consists of two parallel infinite half-lines joined by a straight segment?



Solution:



** click and open the photo for better view.

Concrete Mathematics Exercise Solution: Chapter 1[problem no: 11].

Question: 1.11: A Double Tower of Hanoi contains 2n disks of n different sizes, two of each size. As usual, we’re required to move only one disk at a time, without putting a larger one over a smaller one.

  1. How many moves does it take to transfer a double tower from one peg to another, if disks of equal size are indistinguishable from each other?
  2. What if we are required to reproduce the original top-to-bottom order of all the equal-size disks in the final arrangement? [Hint: This is difficult–it’s really a “bonus problem.”]

Solution:

for a: 


for b: 




** click and open the photo for better view.

Concrete Mathematics Exercise Solution: Chapter 1[problem no: 2].

Question: 1.2: Find the shortest sequence of moves that transfers a tower of n disks from the left peg A to the right peg B, if direct moves between A and B are disallowed. (Each move must be or to or from the middle peg. As usual, a larger disk must never appear above a smaller one.)



Solution:


** click and open the photo for better view.

Sunday, 2 March 2014

Difference between “for” loop and “while” loop?

Topic
For
While
Main margin:
Generally runs till a predetermined iteration.
Loops until a condition meets.

Boolean substitution:
Not possible in general sense.
One of the best feature of while is Boolean (true/false) checking condition. e.g:

while ( x--){}


Accessory
variables:
Is a must creating 1 new variable.
No needed most of the time.

Avatars:
for has 2 types.

1. Regular for loop
for( initialization;  condition; increments/decrements )
{    statements; 
}

2. Alternative notation in Java 5
for( element type name :  collection )
{    statements;
// loop will execute each element 
}

Has while and do-while.
Speed:
Probably for loop, may be because it looks out for 1 specific item through statement body.

while loop has to check conditions for each run until the condition satisfy(ies).




Thursday, 13 February 2014

UVA sloved: problem number: 10579 - Fibonacci Numbers

10579 - Fibonacci Numbers
Time limit: 3.000 seconds

Problem E: Fibonacci Numbers

A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.
f (1) = 1, f (2) = 1, f (n > 2) = f (n - 1) + f (n - 2)

Input and Output 

Your task is to take numbers as input (one per line), and print the corresponding Fibonacci number.

Sample Input 

3
100

Sample Output 

2
354224848179261915075

Note: No generated fibonacci number in excess of 1000 digits will be in the test data, i.e. f (20) = 6765 has 4 digits..
Local_UVa'2003
_____________________________________________________________________

solution: 

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

public class Main

{
    public static void main(String[] args)
    {
      Scanner input= new Scanner(System.in);
      int x;
      
      while(input.hasNext())
      {
          x=input.nextInt();
          BigInteger h=BigInteger.ONE;
          BigInteger m=BigInteger.ONE;
          BigInteger sum=BigInteger.ZERO;
          while(x>2)
          {
              sum=h.add(m);
              m=h;
              h=sum;
              x--;
          }
       System.out.println(sum);
      }
    }
}


/* had a little help from a big pal.*/

Wednesday, 12 February 2014

UVA sloved: problem number: 11448 - Who said crisis?

11448 - Who said crisis?
Time limit: 10.000 seconds 
 B. Who Said Crisis? 

Context

The company Assistance for Critical Moments (ACM) is helping other companies to overcome the current economical crisis. As experts in computing machinery, their job is to calculate the cost/benefit balance of the other companies. They receive two numbers, indicating the total amount of benefits and costs, and they have to compute the final balance.

The Problem

You have to solve the complex business problem of computing balances. You are given two positive integer numbers, corresponding to the benefits and the costs. You have to obtain the total balance, i.e., the difference between benefits and costs.

The Input

The first line of the input contains an integer indicating the number of test cases.
For each test case, there is a line with two positive integer numbers, A and B, corresponding to the benefits and the costs, respectively. Both numbers are between 0 and a googol (10100) to the power of a hundred.

The Output

For each test case, the output should consist of a line indicating the balance: A-B.

Sample Input

4
10 3
4 9
0 8
5 2

Sample Output

7
-5
-8
3

_________________________________________________________________


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

public class Main
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
BigInteger x,y;
int c,d;

c= input.nextInt();
d= 0;

while(c>d)
{
x= input.nextBigInteger();
y= input.nextBigInteger();
System.out.println(x.subtract(y));
d++;
}
}
}


Saturday, 8 February 2014

UVA sloved: problem number: 10110 - Light, more light

10110 - Light, more light

Time limit: 3.000 seconds
Light, more light

The Problem

There is man named "mabu" for switching on-off light in our University. He switches on-off the lights in a corridor. Every bulb has its own toggle switch. That is, if it is pressed then the bulb turns on. Another press will turn it off. To save power consumption (or may be he is mad or something else) he does a peculiar thing. If in a corridor there is `n' bulbs, he walks along the corridor back and forth `n' times and in i'th walk he toggles only the switches whose serial is divisable by i. He does not press any switch when coming back to his initial position. A i'th walk is defined as going down the corridor (while doing the peculiar thing) and coming back again. Now you have to determine what is the final condition of the last bulb. Is it on or off?
 

The Input

The input will be an integer indicating the n'th bulb in a corridor. Which is less then or equals 2^32-1. A zero indicates the end of input. You should not process this input.

The Output

Output "yes" if the light is on otherwise "no" , in a single line.

Sample Input

3
6241
8191
0

Sample Output

no
yes
no



by-
Sadi Khan
Suman Mahbub
01-04-2001

_______________________________________________

solution:

#include<stdio.h>
#include<math.h>

int main()
{
long n, a, b;
while(scanf("%lld", &n)==1)
{
if(n==0)
break;
a= sqrt(n);
b=a*a;

if(b==n)
printf("yes\n");
else
printf("no\n");
};
return 0;
}