jueves, 15 de marzo de 2012

Project Euler 40

#include <iostream>
#include <math.h>
using namespace std;
//metodo para obtener el i-esimo caracter de la concatenacion de 1,2,3,4,5...
int d(int i)
{
    int cd = 0; //cantidad de digitos
    int cda = 0; //cantidad de digitos acumulados
    int scd = 0; //siguiente cantidad de dtitos
    while( cda + scd < i)
    {
        cda += scd;
        cd++;
        scd =  (pow(10,cd)-pow(10,cd-1))*cd;
    }
    int numactual = pow(10,cd-1)-1;//numero actual hasta el cual se sumo
    while(cda < i)
    {
        cda += cd;
        numactual++;
    }
    int csp = (cda-i);
    while(csp--) numactual /= 10;
    return numactual%10;
}
int main()
{
    cout << d(1)*d(10)*d(100)*d(1000)*d(10000)*d(100000)*d(1000000)<<endl;
    return 0;
}

martes, 13 de marzo de 2012

Project Euler 39

#include <iostream>
using namespace std;
int main()
{
    int v[1001], a, b, c;
    for( a = 1000 ; a >= 0 ; a--) v[a] = 0 ;
    for ( c = 1; c <= 1000; c++)
        for ( b = 1; b < c; b++)
            for ( a = 0; a <= b; a++)
                if(a+b>c && b-a<c && a+b+c <= 1000 && c*c == a*a+b*b)
                    v[a+b+c]++;
    b = 0 ;
    for ( a = 0; a <= 1000; a++)
        if(v[a]>v[b])
            b = a;
    cout << b << endl;
    return 0;
}

Project Euler 38

public class Problema38 {
 static boolean isPandigital(String num){
  if(num.length()!=9)
   return false;
  for (int i = 1; i < 10; i++) 
   if(!num.contains(""+i))
    return false;
  return true;
 }
 public static void main(String[] args) {
  String max = "123456789";
  for (int i = 9; i < 10000; i++) {
   int m = 1;
   String sol = "";
   while(sol.length()<9){
    sol += (i*m);
    m++;
   }
   if(sol.length()==9)
    if(isPandigital(sol))
     if(sol.compareTo(max)>0)
      max = sol;
  }
  System.out.println(max);
 }
}