Archive

Archive for November, 2010

A simple program to permutate the array elements


Here is a very simple way to permute the given array elements.


/**
*
* @author Vasanth Raja Chittampally
*/

public class Permutation {

/**
* @param args the command line arguments
*/
void printArray(int []a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");

}
System.out.println("");
}
void permute(int []a,int k ) {
if(k==a.length)
printArray(a);
else
for (int i = k; i < a.length; i++) {
int temp=a[k];
a[k]=a[i];
a[i]=temp;
permute(a,k+1);
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}
public static void main(String[] args) {
Permutation p=new Permutation();
int a[]={1,2,3,4,5,6};
p.permute(a, 0);
}

}

Simply edit the array elements(add elements if you want or delete) still it works. I feel the program is self explanatory if you have any doubts please leave a comment.

Advertisements

Program to find practical numbers


In number theory, a practical number or panarithmic number is a positive integer n such that all smaller positive integers can be represented as sums of distinct divisors of n. For example, 12 is a practical number because all the numbers from 1 to 11 can be expressed as sums of its divisors 1, 2, 3, 4, and 6: as well as these divisors themselves, we have 5=3+2, 7=6+1, 8=6+2, 9=6+3, 10=6+3+1, and 11=6+3+2.

The sequence of practical numbers

1, 2, 4, 6, 8, 12, 16, 18, 20, 24, 28, 30, 32, 36, 40, 42, 48, 54, ….

Here is the Java code to find practical numbers in between given sequence.

/**
*
* @author Vasanth Raja Chittampally
*/
public class PracticalNumbers {
public int[] getPracticalNumbers(int from, int to) {
int a[]=new int[to-from];
int indx=0;
out:for (int i = from; i<=to;i++) {
int sum=0;
for(int j=1;j<=i/2;j++) {
if(i%j==0) {
if(sum<j-1)
continue out;
sum+=j;
}
}
if(sum>=i-1) {
a[indx++]=i;
}
}
int count=0;
for (int i = 0; i < a.length; i++) {
if(a[i]!=0)
count++;
}
int ans[]=new int[count];
for (int i = 0; i <count; i++) {
ans[i]=a[i];
}
return ans;

}

//  You could use this sample code to test function
//  Following main fucntion contains 3 representative test cases
public static void main(String[] arg) {
PracticalNumbers pn = new PracticalNumbers();
//1st test case
int[] res = pn.getPracticalNumbers(1, 20);
for (int i = 0; i < res.length; i++)
System.out.print(res[i] + " ");
System.out.println();
//2nd test case
int[] res2 = pn.getPracticalNumbers(8000, 8200);
for (int i = 0; i < res2.length; i++)
System.out.print(res2[i] + " ");
System.out.println();

//3rd test case
int[] res3 = pn.getPracticalNumbers(1000, 1120);
for (int i = 0; i < res3.length; i++)
System.out.print(res3[i] + " ");
System.out.println();
}
}

Here is the online ide where you can see the output

I feel the above program is self explanatory. If you have any doubts at any line please leave a comment. I’ll clarify your doubts. Thank you. I always welcome your comments or suggestions.

SDD-1 Algorithm Implementation Project


Here is the report I prepared for my Advanced Database Systems course. Our course includes two projects to be completed before awarding the grade. One is Design project and the second one is implementation project. I’ve chosen “Crawler Based search Engine” for the design project andSDD-1 Distributed Database Query Optimization algorithm for my Implementation. Today I finished with the presentation of my implementation and my work. The presentation includes the ppt, report and submitting the source code. In my case they know that the source for SDD-1 is available online(But I’ve not used), they took my code and analyzed with already existing source code, Since I did totally on my own there was no problem at all. I resumed with my presentation and then showed a brief Demo. I liked this kind of evaluation, giving marks to only those who deserve. If it is found copied they will not hesitate to fail or give zero.

ArrayList and Iterator example


Here I want to share with you, the ArrayList and Iterator example. ArrayList is present in util package( java.util.ArrayList) means that we have to import before using it in our program. (import java.util.ArrayList) same as the case with Iterator too (import java.util.Iterator).

ArrayList and Iterator come very handy when we are dealing with a very big projects. Instead using static arrays we can easily, dynamically, create an array as a list and add reference of the elements to that. ArrayList can only take reference of the objects( I mean that we cannot give double, float or int as a reference instead we can use Wrapper classes such as Integer Double etc.,)

ArrayList class provides methods for basic array operations:

  • add( Object o ) – puts reference to object into ArrayList
  • get( int index ) – retrieves object reference from ArrayList index position
  • size() – returns ArrayList size
  • remove( int index ) – removes the element at the specified position in this list. Shifts any subsequent elements to the left and returns the element that was removed from the list.
  • indexOf( Object o) – finds the index in this list of the first occurrence of the specified element
  • clear() – removes all of the elements

Example program:

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Vasanth Raja Chittampally
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ArrayList al = new ArrayList(20); //Exactly equal to ArrayList  al = new ArrayList(20)
        for (int i = 0; i < 20; i++) {
            al.add(""+i);
        }
        Iterator i=al.iterator();

        while(i.hasNext()) {
            System.out.println(""+i.next());
        }
        System.out.println("In the second iteration");
        Iterator i1=al.iterator();
        System.out.println(""+i1);
         while(i1.hasNext()) {
            System.out.println(i1.next());
        }

    }

}

If we don’t provide any size in ArrayList al = new ArrayList(); it takes 10 as ArrayList size. Here in the above program Iterator takes the ArrayList and iterates from starting to ending in a forward direction. Ouput: it prints 0 to 19 two times. ArrayLists cannot work with the multiple threads.. Click here to read my previous post talking about the synchronization of ArrayList

IEEE paper format


Here is the IEEE paper format. You can download and you can directly edit the document. I found this document somewhere in the net.Sample IEEE Paper

David McCandless: The beauty of data visualization


Categories: Uncategorized

Aspirations 2020 Infosys Programming contest


Infosys aspirations 2020

Infosys Aspirations 2020

Aspirations 2020 was a wonderful programming contest event, with a great cause of preparing programming enthus to participate in “ACM” programming contest(Considered as Olympics for programming).  The initiative taken by Infosys is very applaud-able.  Dinesh Kumar Chaudari, Venu Gopal Vajjala(vvgrao) and me participated in the State final round. This event boosted our competence levels high. It was so encouraging to be in State final round after three initial rounds(Teaser, Inter college and Zonal rounds). Before to this we have once visited Infosys as a part of “Spark” program. It was great experience of coming in contact with the other programming enthus. On that day, in a beautiful Infosys campus, we were offered a delicious food. The talks from Infosys HR and other higher officials were very inspiring. We bagged two prizes, one cup, valuable certificates and full of enthusiasm from Infosys. Thanks to Infosys for providing such a wonderful platform to showcase students’ programming skills.