viernes, 10 de febrero de 2012

Project Euler 19

import java.util.Calendar;
public class problem19
{
 public static void main(String[] args)
 { 
  Calendar cal = Calendar.getInstance();
  int c =0;
  for (int i = 1901; i < 2001; i++)
  {
   for (int j = 1; j <13 ; j++)
   {
    cal.set(i, j, 1);
    if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
    c++;
   }
   System.out.println(c);
  }
 }
}

jueves, 9 de febrero de 2012

Project Euler 37

import java.util.BitSet;
import java.util.Scanner;
public class problem37{
 static BitSet a ;

 public static void main(String[] args){
  int c = 0;
  Scanner sc = new Scanner (System.in);

  int n = sc.nextInt(),
    i = 2,
    j = 0;

  a = new BitSet(n);

  for(i = 2 ; (i*i) <= n ; i = i+1)
   if(!a.get(i))
    for(j=i+i;j<=n;j=j+i)
     a.set(j);

  for(i=11;i<=n;i++)
   if(!a.get(i)){
    String aux = Integer.toString(i);
  
    if(check1(i)&&check2(i)&& aux.charAt(0)!='1'&&aux.charAt(aux.length()-1)!='1')
     c=c+i;
   
   }
  System.out.println(c);

 }

 public static boolean check1(int n){
  String num = Integer.toString(n);

  while(true) {

   num = num.replaceFirst("[0-9]","");

   if(num.equals(""))
    break;

   if(!a.get(Integer.parseInt(num)))
    continue;
   else
    return false;

  }
  return true;
 }

 public static boolean check2(int n) {
  String num = Integer.toString(n);

  while(true) {
   int leng =num.length();
   leng--;
   num = num.substring(0,leng );

   if(num.equals(""))
    break;

   if(!a.get(Integer.parseInt(num)))
    continue;
   else
    return false;

  }
  return true;
 }
}

Project Euler 29

import java.math.BigInteger;
import java.util.TreeSet;

public class Problem29 {
 public static void main(String[] args) {
  TreeSet<BigInteger> arbol = new TreeSet<BigInteger>();
  int a,b;
  for (a = 2; a <=100; a++) 
   for (b = 2; b <=100; b++) 
    arbol.add(new BigInteger(a+"").pow(b));
  System.out.println(arbol.size());
 }
}

Project Euler 28

#include <iostream>

using namespace std;
int sum(int L)
{
    int n = L*L;
    int sum = n,
        res = L-1,
        i;
    while(n>=1 && res >=0)
    {
        for(i = 1; i<=4 && res>1 ; i++)
        {
            n = n-res;
            sum += n;
        }
        res-=2;
    }
    return sum;
}
int main()
{
    cout << sum(1001)<< endl;
    return 0;
}

Project Euler 17

public class problem17 {
 static int co = 0;

 public static void main(String[] args) {
  String u[] = { "", "one", "two", "three", "four", "five", "six",
    "seven", "eight", "nine", "ten", "eleven", "twelve",
    "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
    "eighteen", "nineteen", "twenty" };

  String d[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty",
    "seventy", "eighty", "ninety" };

  for (int i = 1; i <= 1000; i++) {
   String num = Integer.toString(i);
   if (num.length() == 1) {
    co = co + u[i].length();
   }
   if (num.length() == 2) {
    if (i < 20) {
     co = co + u[i].length();
    } else {
     co = co + d[num.charAt(0) - 48].length();
     co = co + u[num.charAt(1) - 48].length();
    }
   }
   if (num.length() == 3) {
    String aux = "hundredand";
    co = co + (u[num.charAt(0) - 48].length() + aux.length());
    if (Integer.parseInt(num.substring(1, 3)) < 20) {
     co = co + u[Integer.parseInt(num.substring(1, 3))].length();
    } else {
     co = co + d[num.charAt(1) - 48].length();
     co = co + u[num.charAt(2) - 48].length();
    }
   }
   if (num.length() == 4) {
    co = co + "onethousand".length();
   }
  }
  System.out.println(co - 27);
 }
}

Project Euler 27

public class Problem27 {
 static boolean[] criba;
 public static void main(String[] args) {
  generarCriba(10000000);
  int a=0,b=0,n=0,max = 0;
  long producto=0;
  try{
  for(a = -1000 ; a<=1000;a++)
   for(b = -1000 ; b<=1000;b++){
    int c = 0;
    for(n = 0 ; ;n++){
     int f = fun(n,a,b);
     if(!criba[f])
      c++;
     else
      break;
    }
    if(c>max){
     max = c;
     producto = a*b;
    }
     
   }
  }catch (Exception e) {
   System.out.println("Fallo en :");
   System.out.println(a+"  "+b+"  "+n);
   return;
  }
  System.out.println(producto);   
 }
 public static int fun(int n, int a,int b){
  return Math.abs(n*n+a*n+b);
 }
 public static void generarCriba(int n){
  criba = new boolean[n+1];
  criba[0] = criba[1] = true;
  for (int i = 2; i*i <= n; i++) 
   if(!criba[i])
   for (int j = i+i; j <= n; j+=i) 
    criba[j] = true;
 }
}

Project Euler - 26

Bueno aquí mi primer código para el blog es el problema 26 de projecteuler.net , por pensar en resolverlo rápido tarde 3 días en darme cuenta de que solo era aplicar lo que nos enseñan en primaria, creo que aprendí una gran lección:"No todos los problemas tienen que tener soluciones largas y complicadas."
#include <iostream>

using namespace std;

bool esPeriodico(int n)
{
    while((n&1)==0) n >>= 1;

    while(n%5==0) n /= 5;

    if(n!=1)
        return true;
    return false;
}
int cantidad(int divisor)
{
    int restos[10000]= {0};
    bool pasados[10000]= {0};
    int pos = 1;
    int contador = 0;
    int dividendo;
    restos[0] = 10;
    dividendo = 10;
    while(!pasados[dividendo])
    {
        pasados[dividendo] =  true;
        restos[pos++] = dividendo;
        while(dividendo < divisor)
        {
            dividendo *= 10;
            pasados[dividendo] =  true;
            restos[pos++] = dividendo;
            contador++;
        }
        dividendo =(dividendo%divisor)*10;
        restos[pos++] = dividendo;
        contador++;
    }
    int i = 0;
    while(restos[i]!=dividendo && i < 10000)
        i++;
    if(i<pos)
        return (contador-i+1);
    return contador;
}
int main()
{
    int max , d,aux;
    for(int i = 3; i<=1000; i++)
        if(esPeriodico(i))
        {
            aux = cantidad(i);
            if(aux>max)
            {
                max = aux;
                d = i;
            }
        }
    cout << "El d con el ciclo mas grande es " << d << " con un ciclo de "<<max<< endl;
    return 0;
}