Pages

Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

Wednesday, September 9, 2009

Euler Problem 42 solution

Time (s): ~0.067
package margusmartseppcode.From_40_to_49;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map;

public class Problem_42 {
 static int sumStringChars(String s) {
  if (s == null)
   return 0;
  int n = s.length(), sum = 0;
  for (int i = 0; i < n; i++)
   sum += s.charAt(i) - '@';
  return sum;
 }

 public static void main(String[] args) throws FileNotFoundException {
  Map<Integer, Integer> map = new HashMap<Integer, Integer>();
  String raw = new Scanner(new File("words.txt")).next();
  String str[] = raw.substring(1, raw.length() - 1).split("\",\"");
  int count = 0;

  for (String s : str) {
   int tmp = sumStringChars(s);
   map.put(tmp, map.containsKey(tmp) ? map.get(tmp) + 1 : 1);
  }

  for (Integer i : map.keySet())
   if ((((Math.sqrt(1 + 8 * i) - 1) / 2) % 1 == 0))
    count += map.get(i);

  System.out.println(count);
 }
}

Euler Problem 37 solution

Time (s): ~0.599
package margusmartseppcode.From_30_to_39;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Problem_37 {

 static Integer cint(String nr) {
  return Integer.parseInt(nr);
 }

 static boolean isLTP(String mem, Set<Integer> primes) {
  int n = mem.length();
  if (n < 1)
   return true;
  return primes.contains(cint(mem)) && isLTP(mem.substring(1), primes);
 }

 static boolean isRTP(String mem, Set<Integer> primes) {
  int n = mem.length();
  if (n < 1)
   return true;
  return primes.contains(cint(mem))
    && isRTP(mem.substring(0, n - 1), primes);
 }

 static boolean isTP(String mem, Set<Integer> primes) {
  return isLTP(mem, primes) && isRTP(mem, primes);
 }

 private static void bTP(Integer mem, Set<Integer> primes,
   Set<Integer> truncatable) {
  if (primes.contains(mem)) {
   if (isTP(""+mem, primes))
    truncatable.add(mem);
   TruncatablePrimes(mem, primes, truncatable);
  }
 }

 private static void TruncatablePrimes(Integer elem, Set<Integer> primes,
   Set<Integer> truncatable) {
  String[] o = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  String s = "" + elem;

  for (String pos : o) {
   bTP(cint(s + pos), primes, truncatable);
   bTP(cint(pos + s), primes, truncatable);
  }
 }

 public static void main(String[] args) throws FileNotFoundException {
  Set<Integer> truncatable = new HashSet<Integer>();
  Set<Integer> primes = new HashSet<Integer>();
  Scanner sc = new Scanner(new File("primes1m.txt"));
  int sum = 0;

  for (String tmp = sc.next(); sc.hasNext(); tmp = sc.next())
   primes.add(Integer.parseInt(tmp));

  for (Integer elem : new Integer[] { 3, 7 })
   TruncatablePrimes(elem, primes, truncatable);

  for (Integer elem : truncatable)
   sum += elem;

  System.out.println(sum);
 }
}

Euler Problem 35 solution

Time (s): ~0.921
package margusmartseppcode.From_30_to_39;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Problem_35 {
 static boolean contains_024568(final char[] input) {
  int n = input.length;
  for (int i = 4; i < n; i++)
   if (input[i] == '0' || input[i] == '2' || input[i] == '4'
     || input[i] == '5' || input[i] == '6' || input[i] == '8')
    return true;
  return false;
 }

 static void rotate(StringBuilder sb) {
  sb.append(sb.charAt(0)).delete(0, 1);
 }

 static int iRotate(StringBuilder s) {
  return Integer.parseInt(s.append(s.charAt(0)).delete(0, 1).toString());
 }

 static void CircularPrimes(Integer elem, Set<Integer> primes,
   Set<Integer> found, Set<Integer> circular) {
  StringBuilder sb = new StringBuilder("" + elem);
  ArrayList<Integer> tmp = new ArrayList<Integer>();
  int n = sb.length(), i;

  if (found.contains(elem) || circular.contains(elem))
   return;
  for (i = 1; i <= n; i++)
   tmp.add(iRotate(sb));
  for (Integer mem : tmp)
   if (!primes.contains(mem)) {
    found.addAll(tmp);
    return;
   }

  circular.addAll(tmp);
 }

 public static void main(String[] args) throws FileNotFoundException {
  Set<Integer> circular = new HashSet<Integer>();
  Set<Integer> primes = new HashSet<Integer>();
  Set<Integer> found = new HashSet<Integer>();
  Scanner sc = new Scanner(new File("primes1m.txt"));

  for (String tmp = sc.next(); sc.hasNext(); tmp = sc.next())
   if (!contains_024568(tmp.toCharArray()))
    primes.add(Integer.parseInt(tmp));

  for (Integer elem : primes)
   CircularPrimes(elem, primes, found, circular);

  System.out.println(circular.size());
 }
}

Tuesday, September 8, 2009

Euler Problem 22 solution

Time (s): ~0.096
package margusmartseppcode.From_20_to_29;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Problem_22 {
 public static void main(String[] args) throws FileNotFoundException {
  String raw = new Scanner(new File("names.txt")).next();
  String str[] = raw.substring(1, raw.length() - 1).split("\",\"");
  long ls = 0, ts = 0;
  Arrays.sort(str);

  for (int i = 0; i < str.length; i++, ls = 0) {
   for (char c : str[i].toCharArray())
    ls += c - '@';

   ts += ls * (i + 1);
  }
  System.out.println(ts);
 }
}