Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

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.*/

Saturday, 4 January 2014

skeleton code: real number comperation using Conditional Operator in Java ( ? : ) [ simple, user friendly ]


import java.util.Scanner;

class grps{
    public static void main(String[] args){
        Scanner input= new Scanner(System.in);
       
        System.out.print("enter the first input: ");
        double a=input.nextDouble();
       
        System.out.print("enter the nextinput: ");
        double b=input.nextDouble();
       
        System.out.println(a>b ? "First one is the bigger.":"Second one is the bigger."); //( ? : )
    }
}



raw code: Compound Interest calculation in Java [ simple, user friendly]

Q: lets say, you to find out what will be the number of viewers in my blog in next 30 days. given that, for instance i have 10,000 views now, increasing rate is 1% each day. you have to calculate it for next 30 days and show the result in every other 10th day.
formula is: amount= pri(1+rate)^time

=>
import java.util.Scanner;

class grps{
    public static void main(String[] agrs){
        Scanner input= new Scanner(System.in);
        int y=0;
       
        System.out.print("enter the sum you have already: ");
        double principal=input.nextDouble(); //=10000
       
        System.out.print("how much is the growth rate: ");
        double rate=input.nextDouble();    //=.01
       
        System.out.print("for how many days you want to calculate: ");
        double day=input.nextDouble();    //=30
       
        System.out.print("how often you want to see the result [e.g: 1 being every day, 2 being every other day]: ");
        double show=input.nextDouble();    //=10
       
        for(int x=1; x<=day; x+=show){
            y+=(int)(principal*Math.pow(1+rate, day));
            System.out.println("In "+x+" days there will be "+y+" followers!");
        }
    }
}