Wednesday, 25 September 2013

Factoring: number of ways to factorize a number.

It happens that you learn a new thing or two everyday. Today, I learned how to find the number of ways to factorize a number where order of divisors is not important (4 X 2 is same as 2 X 4).

Let's think about a the problem :

Suppose your number is n. Also, n can be factorized as n = a x b. So, the number of ways to factorize n will be equal to 1 + no. of ways to factorize b !

So, we need a recursive approach. Here's a code in C++ for doing such that:




//This code finds the no. of ways to factorise a number.
//It is based on a recursve code.
//Order is not important i.e; 2 X 4 is same as 4 X 2.
#include <cstdio>
#include <iostream>
using namespace std;

int countWays(int n,int factor)
{
   int i;
   int count=0; //This variable will contain the number of ways
  for(i=factor;i*i<=n;i++)
    if(n%i==0)
     count += countWays(n/i,i) + 1;
 return count;    
}  
int main()
{
    int n,t;
 scanf("%d",&t); //Test cases.
 while(t--)
 {
  scanf("%d",&n);
  printf("%d\n",countWays(n,2));
 }
 return 0;
}

Try to understand it ..its very simple !

Monday, 29 July 2013

Computer Science: A Beginning

So, here you are !
You are reading this either because you are a Computer Science student or want to be one.
Great...so, here I go.

First about computers. Computers make our job easier.
But, how ?

You will explore only this question in the fantastic realm of Computer Science !

But, becoming a good Computer scientist requires immense efforts. Also, you have to become good in the following things.

First and foremost,  you have to develop a sequential thinking pattern (Computers are sequential machines, they execute in discrete steps. They are not like you :- you have a brain which acts as a parallel machine).

Second, you have to get a good grip on a particular computer language which will be your tool for instructing computers what to do.

Third, you have to learn programming and know how some real world problems are solved on computers. The area is vast, but you still have to know some of the basic ideas.

Fourth, practice.... lots and lots of practice. Browse problems, read articles, discuss with your friends and most importantly, remain excited about computers !

Only this much for now. I will expand it if I feel the need to. Till then , good luck !