rss
Twitter Delicious Facebook Digg Stumbleupon Favorites
Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Tuesday, October 11, 2011

Write a program to find average of consecutive N Odd number and Even number.


In mathematics, the parity of an object states whether it is even or odd.
This concept begins with integers. An even number is an integer that is "evenly divisible" by 2, i.e., divisible by 2 without remainder; an odd number is an integer that is not evenly divisible by 2. (The old-fashioned term "evenly divisible" is now almost always shortened to "divisible".) A formal definition of an even number is that it is an integer of the form n = 2k, where k is an integer; it can then be showed that an odd number is an integer of the form n = 2k + 1. An even number has the form n = 2k where k is an integer.
Examples of even numbers are −4, 8, and 1728. Examples of odd numbers are −5, 9, 3, and 71. This classification only applies to integers, i.e., a fractional number like 1/2 or 4.201 is neither even nor odd.
The sets of even and odd numbers can be defined as following:
  • Even = \{ 2k; \forall k \in \mathbb{Z} \}
  • Odd = \{ 2k+1; \forall k \in \mathbb{Z} \}
A number (i.e., integer) expressed in the decimal numeral system is even or odd according to whether its last digit is even or odd. That is, if the last digit is 1, 3, 5, 7, or 9, then it's odd; otherwise it's even. The same idea will work using any even base. In particular, a number expressed in the binary numeral system is odd if its last digit is 1 and even if its last digit is 0. In an odd base, the number is even according to the sum of its digits – it is even if and only if the sum of its digits is even.

class EvenOdd{
      public static void main(String args[]){
      int n = Integer.parseInt(args[0]);
      int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;
      while(n > 0){
           if(n%2==0){
               cntEven++;
               sumEven = sumEven + n;
           }
           else{
               cntOdd++;
               sumOdd = sumOdd + n;
           }
           n--;
      }
      int evenAvg,oddAvg;
      evenAvg = sumEven/cntEven;
      oddAvg = sumOdd/cntOdd;
      System.out.println("Average of first N Even no is "+evenAvg);
      System.out.println("Average of first N Odd no is "+oddAvg);

  }
}

Saturday, October 8, 2011

A program to find whether number is palindrome or not.

A palindromic number or numeral palindrome is a 'symmetrical' number like 16461, that remains the same when its digits are reversed. The term palindromic is derived from palindrome, which refers to a word like rotor that remains unchanged under reversal of its letters. The first palindromic numbers (in decimal) are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, ….
Palindromic numbers receive most attention in the realm of recreational mathematics. A typical problem asks for numbers that possess a certain property and are palindromic. For instance,
  • the palindromic primes are 2, 3, 5, 7, 11, 101, 131, 151, …
  • the palindromic square numbers are 0, 1, 4, 9, 121, 484, 676, 10201, 12321, …
Buckminster Fuller referred to palindromic numbers as Scheherazade numbers in his book Synergetics, because Scheherazade was the name of the story-telling wife in the 1001 Nights.
It is fairly straightforward to appreciate that in any base there are infinitely many palindromic numbers, since in any base the infinite sequence of numbers written (in that base) as 101, 1001, 10001, etc. (in which the nth number is a 1, followed by n zeros, followed by a 1) consists of palindromic numbers only.
Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number n > 0 in base b ≥ 2, where it is written in standard notation with k+1 digits ai as:
n=\sum_{i=0}^ka_ib^i
with, as usual, 0 ≤ ai < b for all i and ak ≠ 0. Then n is palindromic if and only if ai = aki for all i. Zero is written 0 in any base and is also palindromic by definition.
class PalindromeNumbers{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);
          int n = num;
          int reverse=0,remainder;
          while(num > 0){
                remainder = num % 10;
                reverse = reverse * 10 + remainder;
                num = num / 10;
           }
          if(reverse == n)
              System.out.println(n+" is a Palindrome Number");
          else
              System.out.println(n+" is not a Palindrome Number");
     }
}

A program to Find whether number is Prime or Not.

A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. For example, 5 is prime, since no number except 1 and 5 divides it. On the other hand, 6 is not a prime (it is composite), since 6 = 2 × 3. The fundamental theorem of arithmetic establishes the central role of primes in number theory: any positive integer n can be expressed as the product of powers of primes in a way that is unique except for a possible reordering of the factors. This theorem requires excluding 1 as a prime. There are infinitely many primes, as demonstrated by Euclid around 300 BC.
The property of being prime is called primality. A simple but slow method of verifying the primality of a given number n is known as trial division. It consists of testing whether n is a multiple of any integer between 2 and n. Algorithms that are much more efficient than trial division have been devised to test the primality of large numbers. Particularly fast methods are available for primes of special forms, such as Mersenne primes. As of 2011, the largest known prime number has about 13 million decimal digits.
There is no known useful formula that yields all of the prime numbers and no composites. However, the distribution of primes, that is to say, the statistical behaviour of primes in the large, can be modeled. The first result in that direction is the prime number theorem which says that the probability that a given, randomly chosen number n is prime is inversely proportional to its number of digits, or the logarithm of n. Therefore, the density of prime numbers within natural numbers is 0, but in a sense, primes occur more often than squares of integers.

The prime number theorem was proven at the end of the 19th century using methods of analytic number theory. The unproven Riemann hypothesis dating from 1859 implies a refined statement concerning the distribution of primes. Many questions around prime numbers remain open, many of which can be stated simply. For example, Goldbach's conjecture, which asserts that every even integer greater than 2 can be expressed as the sum of two primes, and the twin prime conjecture, which says that there are infinitely many twin primes (pairs of primes whose difference is 2), have been unresolved for more than a century. Such questions spurred the development of various branches of number theory, focusing on analytic or algebraic aspects of numbers.
Primes are applied in several routines in information technology, such as public-key cryptography, which makes use of properties such as the difficulty of factoring large numbers into their prime factors. Prime numbers give rise to various generalizations in other mathematical domains, mainly algebra, such as prime elements and prime ideals.

class PrimeNumbers{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);
         int flag=0;
         for(int i=2;i<num;i++){
             if(num%i==0)
              {
                 System.out.println(num+" is not a Prime Number");
                 flag = 1;
                 break;
              }
         }
         if(flag==0)
             System.out.println(num+" is a Prime Number");
    }
}

A program to find whether given number is Armstrong or not.

In recreational number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong) or a plus perfect number) is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g. b = 10 for the decimal system or b = 2 for the binary system.
The definition of a narcissistic number relies on the decimal representation n = dkdk-1...d1d0 of a natural number n, e.g.
n = dk·10k-1 + dk-1·10k-2 + ... + d2·10 + d1,
with k digits di satisfying 0 ≤ di ≤ 9. Such a number n is called narcissistic if it satisfies the condition
n = dkk + dk-1k + ... + d2k + d1k.
For example the 3-digit decimal number 153 is a narcissistic number because 153 = 13 + 53 + 33.
Narcissistic numbers can also be defined with respect to numeral systems with a base b other than b = 10. The base-b representation of a natural number n is defined by
n = dkbk-1 + dk-1bk-2 + ... + d2b + d1,
where the base-b digits di satisfy the condition 0 ≤ di ≤ b-1. For example the (decimal) number 17 is a narcissistic number with respect to the numeral system with base b = 3. Its three base-3 digits are 122, because 17 = 1·32 + 2·3 + 2 , and it satisfies the equation 17 = 13 + 23 + 23.
If the constraint that the power must equal the number of digits is dropped, so that for some m possibly different from k it happens that
n = dkm + dk-1m + ... + d2m + d1m,
then n is called a perfect digital invariant or PDI. For example, the decimal number 4150 has four decimal digits and is the sum of the fifth powers of its decimal digits
4150 = 45 + 15 + 55 + 05,
so it is a perfect digital invariant but not a narcissistic number.
In "A Mathematician's Apology", G. H. Hardy wrote:
There are just four numbers, after unity, which are the sums of the cubes of their digits:
153 = 13 + 53 + 33
370 = 33 + 73 + 03
371 = 33 + 73 + 13
407 = 43 + 03 + 73.
These are odd facts, very suitable for puzzle columns and likely to amuse amateurs, but there is nothing in them which appeals to the mathematician.

The sequence of "base 10" narcissistic numbers starts: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474 ...
The sequence of "base 3" narcissistic numbers starts: 0, 1, 2, 12, 122
The sequence of "base 4" narcissistic numbers starts: 0, 1, 2, 3, 313
The number of narcissistic numbers in a given base is finite, since the maximum possible sum of the kth powers of a k digit number in base b is
k(b-1)^k\, ,
and if k is large enough then
k(b-1)^k<b^{k-1}\, ,
in which case no base b narcissistic number can have k or more digits.
There are 88 narcissistic numbers in base 10, of which the largest is
115,132,219,018,763,992,565,095,597,973,971,522,401
with 39 digits.
Unlike narcissistic numbers, no upper bound can be determined for the size of PDIs in a given base, and it is not currently known whether or not the number of PDIs for an arbitrary base is finite or infinite.
class ArmstrongNumbers{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      int n = num;
      int check=0,remainder;
      while(num > 0){
           remainder = num % 10;
           check = check + (int)Math.pow(remainder,3);
           num = num / 10;
      }
      if(check == n)
            System.out.println(n+" is an Armstrong Number");
      else
            System.out.println(n+" is not a Armstrong Number");
   }
}

Friday, October 7, 2011

A program to find Fibonacci series of a given number.

In mathematics, the Fibonacci numbers are the numbers in the following integer sequence:
0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots\;
By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
F_n = F_{n-1} + F_{n-2},\!\,
with seed values
F_0 = 0 \quad\text{and}\quad F_1 = 1.
The Fibonacci sequence is named after Leonardo of Pisa, who was known as Fibonacci. Fibonacci's 1202 book Liber Abaci introduced the sequence to Western European mathematics,although the sequence had been described earlier in Indian mathematics. (By modern convention, the sequence begins with F0 = 0. The Liber Abaci began the sequence with F1 = 1, omitting the initial 0, and the sequence is still written this way by some.)
Fibonacci numbers are closely related to Lucas numbers in that they are a complementary pair of Lucas sequences. They are intimately connected with the golden ratio, for example the closest rational approximations to the ratio are 2/1, 3/2, 5/3, 8/5, ... . Applications include computer algorithms such as the Fibonacci search technique and the Fibonacci heap data structure, and graphs called Fibonacci cubes used for interconnecting parallel and distributed systems. They also appear in biological settings, such as branching in trees, arrangement of leaves on a stem, the fruit spouts of a pineapple, the flowering of artichoke, an uncurling fern and the arrangement of a pine cone.
The Fibonacci sequence appears in Indian mathematics, in connection with Sanskrit prosody. In the Sanskrit oral tradition, there was much emphasis on how long (L) syllables mix with the short (S), and counting the different patterns of L and S within a given fixed length results in the Fibonacci numbers; the number of patterns that are m short syllables long is the Fibonacci number Fm + 1.
Susantha Goonatilake writes that the development of the Fibonacci sequence "is attributed in part to Pingala (200 BC), later being associated with Virahanka (c. 700 AD), Gopāla (c.1135 AD), and Hemachandra (c.1150)". Parmanand Singh cites Pingala's cryptic formula misrau cha ("the two are mixed") and cites scholars who interpret it in context as saying that the cases for m beats (Fm+1) is obtained by adding a [S] to Fm cases and [L] to the Fm−1 cases. He dates Pingala before 450 BCE.

However, the clearest exposition of the series arises in the work of Virahanka (c. 700AD), whose own work is lost, but is available in a quotation by Gopala (c.1135):
Variations of two earlier meters [is the variation]... For example, for [a meter of length] four, variations of meters of two [and] three being mixed, five happens. [works out examples 8, 13, 21]... In this way, the process should be followed in all mAtrA-vr.ttas (prosodic combinations).
The series is also discussed by Gopala (before 1135AD) and by the Jain scholar Hemachandra (c. 1150AD).
In the West, the Fibonacci sequence first appears in the book Liber Abaci (1202) by Leonardo of Pisa, known as Fibonacci. Fibonacci considers the growth of an idealized (biologically unrealistic)rabbit population, assuming that: a newly born pair of rabbits, one male, one female, are put in a field; rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits; rabbits never die and a mating pair always produces one new pair (one male, one female) every month from the second month on. The puzzle that Fibonacci posed was: how many pairs will there be in one year?
  • At the end of the first month, they mate, but there is still only 1 pair.
  • At the end of the second month the female produces a new pair, so now there are 2 pairs of rabbits in the field.
  • At the end of the third month, the original female produces a second pair, making 3 pairs in all in the field.
  • At the end of the fourth month, the original female has produced yet another new pair, the female born two months ago produces her first pair also, making 5 pairs.
At the end of the nth month, the number of pairs of rabbits is equal to the number of new pairs (which is the number of pairs in month n − 2) plus the number of pairs alive last month (n − 1). This is thenth Fibonacci number.
The name "Fibonacci sequence" was first used by the 19th-century number theorist Édouard Lucas.

 
class FibonacciSeries{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);               
          System.out.println("*****Fibonacci Series*****");
          int f1, f2=0, f3=1;
          for(int i=1;i<=num;i++){
             System.out.println(" "+f3+" ");
             f1 = f2;
             f2 = f3;
             f3 = f1 + f2;
          }
   }
}

A program to reverse a given number.



class ReverseANumber{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);               
          int remainder, result=0;
          while(num>0){
              remainder = num%10;
              result = result * 10 + remainder;
              num = num/10;
         }
         System.out.println("Reverse number is : "+result);
    }
}

A program to find factorial of given number

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5 ! = 5  \times  4  \times  3  \times  2  \times  1 = 120  \
The value of 0! is 1, according to the convention for an empty product.
The factorial operation is encountered in many different areas of mathematics, notably in combinatorics, algebra and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars. The notation n! was introduced by Christian Kramp in 1808.
The definition of the factorial function can also be extended to non-integer arguments, while retaining its most important properties; this involves more advanced mathematics, notably techniques from mathematical analysis.
The factorial function is formally defined by
 n!=\prod_{k=1}^n k \!
or recursively defined by
 n! = \begin{cases}1 & \text{if } n = 0, \\(n-1)!\times n & \text{if } n > 0.\end{cases}
Both of the above definitions incorporate the instance
0! = 1, \
in the first case by the convention that the product of no numbers at all is 1. This is useful because:
  • There is exactly one permutation of zero objects (with nothing to permute, "everything" is left in place).
  • The recurrence relation (n + 1)! = n! × (n + 1), valid for n > 0, extends to n = 0.
  • It allows for the expression of many formulas, like the exponential function as a power series:
 e^x = \sum_{n = 0}^{\infty}\frac{x^n}{n!}
  • It makes many identities in combinatorics valid for all applicable sizes. The number of ways to choose 0 elements from the empty set is \tbinom{0}{0} = \tfrac{0!}{0!0!} = 1. More generally, the number of ways to choose (all) n elements among a set of n is \tbinom nn = \tfrac{n!}{n!0!} = 1.
The factorial function can also be defined for non-integer values using more advanced mathematics, detailed in the section below. This more generalized definition is used by advanced calculators and mathematical software such as Maple or Mathematica.

class Factors{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);             
          int result = 1;
          while(num>0){
                result = result * num;
                num--;
          }
          System.out.println("Factorial of Given no. is : "+result);
   }
}
Powered by Blogger.