Thursday, 27 February 2014

Programs on arrays

Program to print the Matrix in spiral order

#include<stdio.h>
main()
{
int a[10][10],i,j,k,l,m,n,top,bot,lef,rig;
printf("enter the no of rows in the matrix\n");
scanf("%d",&m);
printf("enter the no of columns in the matrix\n");
scanf("%d",&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
top=0;
bot=m-1;
lef=0;
rig=n-1;
for(j=0;j<n/2||j<m/2;j++)
{
for(i=lef;i<=rig;i++)
printf("%d ",a[top][i]);
top++;
printf("\n");
for(i=top;i<=bot;i++)
printf("%d ",a[i][rig]);
rig--;
printf("\n");
for(i=rig;i>=lef;i--)
printf("%d ",a[bot][i]);
bot--;
printf("\n");
for(i=bot;i>=top;i--)
printf("%d ",a[i][lef]);
lef++;
printf("\n");
}
}
 
TC = Θ(m*n)
   SC = O(1)

Swapping two numbers without using temporary variable

void swap(int *i,int *j)
*i=(*i)^(*j);
*j=(*i)^(*j);
*i=(*i)^(*j);
}

or

void swap(int *i,int *j)
{
*i+=*j;
*j=(*i)-(*j);
*i=(*i)-(*j);
}

Binary search

int binary(int a[],int n,int ele)
{
int m,r=n-1,l=0;
while(l<=r)
{
m=(l+r)/2;
if(a[m]==ele) return m;
else if(a[m]>ele) r=m-1;
else l=m+1;
}
return -1;
}

TC = O(log n)
SC = O(1) 

Given a sorted array which may contain repeated elements, find the left most index of the given number (return -1 if element doesn't exist)

int leftmost(int array[],int n,int ele)
{
int middle,left=0,right=n-1;
while(left<right)
{
middle=(left+right)/2;
if(a[middle]==ele) right=middle;
else if(a[middle]<ele) left=middle+1;
else right=middle-1;
}
if(a[left]==ele)
return left;
else return -1;
}

TC = Θ(log n)
SC = O(1)

An array of size contains all values from 1 to n-1 but one element is repeated. Find the repeated element

int duplicate(int a[],int n)
{
int i,j,res;
for(i=0;i<n;i++)
{
if(a[abs(a[i])]>0)
a[abs(a[i])]*=-1;
else
return abs(a[i]);
}

TC = O(n)
SC = O(1)

An array of size n contains all the elements from 0 to n except one element. Find the missing element

int find_missing(int a[],int n)
{
int i,res=0;
for(i=0;i<n;i++)
{
res=res^a[i];
res=res^(i+1);
}
return res;
}

TC = Θ(n)
SC = O(1)

Check whether the number is palindrome or not

int pal(int n)
{
int i=0;
while(i<n)
{
i*=10;
i+=n%10;
n/=10;
}
if(i==n || n==(i/10))
return 1;
return 0;
}

TC = Θ(log n)
SC = O(1)

Sorting an array with only two values 0 & 1

void sorting(int a[], int n)
{

}

program to print all the prime numbers upto the given number

void primeUptoN(int n)
{
int i=0;
for(i=2;i<=n;i++)
if(prime(i))
printf("%d\t",i);
printf("\n");
}
int  prime(int i)
{
int j;
    for(j=2;j*j<=i;j++)
if(i%j==0)
return 0;
return 1;

}

Tower of Hanoi

void towerhanoi(char x,char y,char z,int n)
{
if(n==1)
printf("move disk from tower %c to %c\n",x,z);
else
{
towerhanoi(x,z,y,n-1);
printf("move disk from tower %c to %c\n",x,z);
towerhanoi(y,x,z,n-1);
}
}

Tuesday, 18 February 2014

Google Logo




 “Why Google used specific colours in their logo?” 

It might be that colors are assigned to letters according to whether their positions represent a prime number or don’t. Thus letters number 1, 2, 3 and 5 (all of them prime numbers) have a distinctive or “prime” color assigned: blue, red, yellow and green. Letters number 4 and 6 (not prime numbers) repeat colors -no longer “prime colors”- in the same order that such colors were assigned in the first time: blue and then red. If the sequence were to continue with new letters, the next letter, number 7, should have a new or “prime color” since 7 is prime; letters number 8, 9 and 10 should repeat colors in the pre established order: yellow, green and the same as 7′s; letter number 11, also a prime number, should have another new color, and so forth.

Beauty Of Maths


1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321

Sequential 1's with 9
1 x 9 + 2 = 11
12 x 9 + 3 = 111
123 x 9 + 4 = 1111
1234 x 9 + 5 = 11111
12345 x 9 + 6 = 111111
123456 x 9 + 7 = 1111111
1234567 x 9 + 8 = 11111111
12345678 x 9 + 9 = 111111111
123456789 x 9 + 10 = 1111111111

Sequential 8's with 9
9 x 9 + 7 = 88
98 x 9 + 6 = 888
987 x 9 + 5 = 8888
9876 x 9 + 4 = 88888
98765 x 9 + 3 = 888888
987654 x 9 + 2 = 8888888
9876543 x 9 + 1 = 88888888
98765432 x 9 + 0 = 888888888

Numeric Palindrome with 1's
1 x 1 = 1
11 x 11 = 121
111 x 111 = 12321
1111 x 1111 = 1234321
11111 x 11111 = 123454321
111111 x 111111 = 12345654321
1111111 x 1111111 = 1234567654321
11111111 x 11111111 =   123456787654321

111111111 x 111111111 = 12345678987654321
Without 8
12345679 x 9 = 111111111
12345679 x 18 = 222222222
12345679 x 27 = 333333333
12345679 x 36 = 444444444
12345679 x 45 = 555555555
12345679 x 54 = 666666666
12345679 x 63 = 777777777
12345679 x 72 = 888888888
12345679 x 81 = 999999999

Sequential Inputs of 9
9 x 9 = 81
99 x 99 = 9801
999 x 999 = 998001
9999 x 9999 = 99980001
99999 x 99999 = 9999800001
999999 x 999999 = 999998000001
9999999 x 9999999 = 99999980000001
99999999 x 99999999 = 9999999800000001
999999999 x 999999999 = 999999998000000001

Sequential Inputs of 6
6 x 7 = 42
66 x 67 = 4422
666 x 667 = 444222
6666 x 6667 = 44442222
66666 x 66667 = 4444422222
666666 x 666667 = 444444222222
6666666 x 6666667 = 44444442222222
66666666 x 66666667 = 4444444422222222
666666666 x 666666667 = 444444444222222222
......................................


Famous Quotes

"As long as algebra is taught in school, there will be prayer in school."
-Cokie Roberts

"Do not worry about your problems with mathematics, I assure you mine are far greater."
- Albert Einstein

"God does arithmetic."
-Karl Friedrich Gauss

"God does not care about our mathematical difficulties. He integrates empirically."
-Albert Einstein

"God made the natural numbers, all the rest is the work of man."
-Leopold Kronecker

"He who can properly define and divide is to be considered a god."
-Plato

"How dare we speak of the laws of chance? Is not chance the antithesis of all law?"
-Bertrand Russell

"I admit that mathematical science is a good thing. But excessive devotion to it is a bad thing."
-Aldous Huxley

"I am accustomed, as a professional mathematician, to living in a sort of vacuum, surrounded by people who declare with an odd sort of pride that they are mathematically illiterate."
-David Mumford

"I don't believe in mathematics."
-Albert Einstein

"I have no faith in political arithmetic."
-Adam Smith

"If I feel unhappy, I do mathematics to become happy. If I am happy, I do mathematics to keep happy."
-Alfréd Rényi

"In great mathematics there is a very high degree of unexpectedness, combined with inevitability and economy."
-G. H. Hardy

"In mathematics you don't understand things. You just get used to them."
-John Von Neumann

"It is easy to lie with statistics. It is hard to tell the truth without it."
-Andrejs Dunkels

"Life is good for only two things, discovering mathematics and teaching mathematics."
-Siméon Poisson

"Mathematical proofs, like diamonds, are hard and clear, and will be touched with nothing but strict reasoning."
-John Locke

"Mathematicians are like lovers. Grant a mathematician the least principle, and he will draw from it a consequence which you must also grant him, and from this consequence another."
-Bernard Le Bouyer de Fontenelle

"Mathematics consists of proving the most obvious thing in the least obvious way."
-George Polya

"Mathematics is a game played according to certain simple rules with meaningless marks on paper."
-David Hilbert

"[Mathematics] is an independent world created out of pure intelligence."
-William Wordsworth

"Mathematics is like checkers in being suitable for the young, not too difficult, amusing, and without peril to the state."
-Plato

"Mathematics is not yet capable of coping with the naïveté of the mathematician himself."
-Abraham Kaplan

"Mathematics is the art of giving the same name to different things."
-Jules Henri Poincaré

"Mathematics is the only instructional material that can be presented in an entirely undogmatic way."
-Max Dehn

"Mathematics is the science of what is clear by itself."
-Carl Jacobi

"Mathematics may be defined as the subject in which we never know what we are talking about, nor whether what we are saying is true."
-Bertrand Russell, referring to the axiomatic method, where certain properties of an (otherwise unknown) structure are assumed and consequences thereof are then logically derived

"Mathematics seems to endow one with something like a new sense."
-Charles Darwin

"Mathematics takes us into the region of absolute necessity, to which not only the actual word, but every possible word, must conform."
-Bertrand Russell
"Measure what is measurable, and make measurable what is not so."
-Galileo Galilei

"Medicine makes people ill, mathematics make them sad and theology makes them sinful."
-Martin Luther

"No human investigation can be called real science if it cannot be demonstrated mathematically."
-Leonardo da Vinci

"Now I feel as if I should succeed in doing something in mathematics, although I cannot see why it is so very important...The knowledge doesn't make life any sweeter or happier, does it?"
-Helen Keller

"One cannot escape the feeling that these mathematical formulas have an independent existence and an intelligence of their own, that they are wiser than we are, wiser even than their discoverers, that we get more out of them than was originally put into them."
-Heinrich Rudolf Hertz

“Perfect numbers like perfect men are very rare."-
René Descartes

"Pure mathematics, may it never be of any use to anyone."
Henry John Stephen Smith

"Sex is the mathematics urge sublimated."
M. C. Reed

"Since the mathematicians have invaded the theory of relativity, I do not understand it myself anymore."
Albert Einstein

"The art of doing mathematics consists in finding that special case which contains all the germs of generality."
David Hilbert

"The good Christian should beware of mathematicians, and all those who make empty prophesies. The danger already exists that the mathematicians have made a covenant with the devil to darken the spirit and to confine man in the bonds of Hell."
St. Augustine

"The imaginary number is a fine and wonderful recourse of the divine spirit, almost an amphibian between being and not being."
Gottfried Wilhelm Leibniz

"The infinite! No other question has ever moved so profoundly the spirit of man."
David Hilbert

"The knowledge of which geometry aims is the knowledge of the eternal."
Plato

"The mathematician has reached the highest rung on the ladder of human thought."
Havelock Ellis

"The science of mathematics presents the most brilliant example of how pure reason may successfully enlarge its domain without the aid of experience."
Immanuel Kant

"The simplest schoolboy is now familiar with facts for which Archimedes would have sacrificed his life."
Ernest Renan

"There are three kinds of lies: lies, damned lies, and statistics."
Benjamin Disraeli

"There is no branch of mathematics, however abstract, which may not some day be applied to phenomena of the real world."
Nikolai Lobachevsky

"In real life there is no such thing as algebra."
Fran Lebowitz

A mathematician is a device for turning coffee into theorems.
-Alfred Renyl & Paul Erdos

Each generation has its few great mathematicians...and [the others'] research harms no one.
-Alfred W Adler in “ Mathematics and creativity"

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.
-G H Hardy in “ A Mathematician’s Apology”

Some of you may have met mathematicians and wondered how they got that way.
-Tom Lehrer

It is impossible to be a mathematician without being a poet in soul.
-Sofia Kovalevskaya

 There are two ways to do great mathematics. The first is to be smarter than everybody else. The second way is to be stupider than everybody else—but persistent.

-Raoul Bott