Sunday, January 20, 2013

Problem 22: Names scores


Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?

Solution:

the solution can be divided into three parts:
  1. storing the names into an appropriate data structure, for this case I used a String array.
  2. Sorting the array, for which you can use any optimal sorting algorithm, merge sort would be the best, I used the Arrays class in java, which is as fast as merge sort.
  3. calculating the score.
now the code is given below:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;


public class Prob22 {


 public static void main(String[] args)throws IOException
 {
  String filePath = "P:\\names.txt";
  String a[] = FileToStrArray(filePath);
  Arrays.sort(a);//sorting the array
  long sum = 0l;
  
  
  
//calculating score    
for(int i =0 ; i < a.length ; i++)
{ a[i] = a[i].replaceAll("\"","" ); //removing " " from each name.
 int NameSum =0; 
 for(int j = 0; j< a[i].length() ; j++)
 {
 NameSum += (a[i].charAt(j)-64);
 }
 NameSum =NameSum*(i+1);
 sum +=NameSum;
 }
  
System.out.println(sum); 
  
 }
 
 
 //function to store the names in a string array.
public static String[] FileToStrArray(String  filePath)throws IOException 
{
 String names[] ={null};
 String S;
 int size = 0;
 try {
  FileReader fr = new FileReader(filePath);
  BufferedReader br = new BufferedReader(fr);
  FileReader fr2 = new FileReader(filePath);
  BufferedReader br2 = new BufferedReader(fr2);
  
  
  while((S=br.readLine())!=null)
  {
   for(int i = 0 ; i < S.length() ; i++)
   {
    if(S.charAt(i)== '"')
     size++;
   }
   size =size/2;
  }
  names = new String[size];
  
  while((S=br2.readLine())!=null)
  {
  names=S.split(",");
  }
  
 
  
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 
 
 return names;
 }
 
 

}

Problem 21: Amicable numbers


Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.

Solution:
it's a very easy problem, Here's the java code:
public class Prob21 {

 

 
 public static void main(String[] args) 
 {
 int sum = 0;
  
 
 for(int i = 2; i < 10000 ; i++)
  {
  
   if(amicable_checker(i))
   { 
    sum+=i;
   }
  }
  
System.out.println(sum);

 }

 
 public static int propDivSumFinder(int n)
 { int sum =0;
 
 for(int i =1; i< n/1 ; i++)
 {
  if(n%i == 0)
   sum += i;
 }
  
  return sum; 
 }
 
 
public static boolean amicable_checker(int a)
 {
  int b;
  b = propDivSumFinder(a);
  if(a!=b)
  {
   if(propDivSumFinder(b) == a)
    {
    
    return true;
    }
    else 
    return false;
  }
  return false;
 }
 
 
 
}