Personal tools

Euler problems/171 to 180

From HaskellWiki

< Euler problems(Difference between revisions)
Jump to: navigation, search
(add problem 171)
Line 87: Line 87:
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
problem_174 = undefined
+
#include <stdio.h>
  +
  +
int main()
  +
{
  +
int a=0;
  +
int c=1000000/4;
  +
int L[1000000];
  +
int i,x,b;
  +
for(i=0;i<=1000000;i++)
  +
{
  +
L[i]=0;
  +
}
  +
  +
for(x=1;x<=500;x++)
  +
{
  +
int q=x*x;
  +
for(b=1;b<=250000;b++)
  +
{
  +
int y=q+b*x;
  +
if(y<=c){L[y*4]++;}
  +
}
  +
}
  +
  +
for(i=0;i<=1000000;i++)
  +
{if(L[i]<=10&&L[i]>=1)
  +
{a++;}
  +
}
  +
  +
printf( "%d\n", a);
  +
return 1;
  +
}
  +
problem_174 = main
 
</haskell>
 
</haskell>
   
Line 147: Line 147:
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
problem_179 = undefined
+
#include <stdio.h>
  +
  +
#define SZ 10000000
  +
int n[SZ + 1];
  +
  +
int main ()
  +
{
  +
int i, j;
  +
for (i = 0; i <= SZ; i++)
  +
n[i] = 1;
  +
  +
for (i = 2; i <= SZ; i++)
  +
for (j = i; j <= SZ; j += i)
  +
n[j] += 1;
  +
  +
j = 0;
  +
for (i = 1; i < SZ; i++)
  +
if (n[i] == n[i + 1])
  +
j++;
  +
  +
printf ("%d\n", j);
  +
return 0;
  +
}
  +
problem_179 = main
 
</haskell>
 
</haskell>
   
 
== [http://projecteuler.net/index.php?section=problems&id=180 Problem 180] ==
 
== [http://projecteuler.net/index.php?section=problems&id=180 Problem 180] ==
+
Rational zeros of a function of three variables.
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>

Revision as of 13:39, 2 February 2008

Contents

1 Problem 171

Finding numbers for which the sum of the squares of the digits is a square.

Solution:

#include <stdio.h>
 
static int result = 0;
 
#define digits 20
static long long fact[digits+1];
static const long long precision      = 1000000000;
static const long long precision_mult =  111111111;
 
#define maxsquare 64 /* must be a power of 2 > digits * 9^2 */
 
static inline int issquare( int n )
{
    for( int step = maxsquare/2, i = step;;)
    {
        if( i*i == n )        return i;
        if( !( step >>= 1 ) ) return -1;
        if( i*i > n )         i -= step;
        else                  i += step;
    }
}
 
static inline void dodigit( int d, int nr, int sum, long long c, int s )
{
    if( d )
        for( int n = 0; n <= nr; c *= ++n, s += d, sum += d*d )
            dodigit( d-1, nr - n, sum, c, s );
    else if( issquare( sum ) > 0 )
        result = ( s * ( fact[digits] / ( c * fact[nr] ) )
                 / digits % precision * precision_mult
                 + result ) % precision;
}
 
int main( void )
{
    fact[0] = 1;
    for( int i = 1; i < digits+1; i++ ) fact[i] = fact[i-1]*i;
    dodigit( 9, digits, 0, 1, 0 );
    printf( "%d\n", result );
    return 0;
}
problem_171 = main

2 Problem 172

Investigating numbers with few repeated digits.

Solution:

factorial n = product [1..toInteger n]
 
fallingFactorial x n = product [x - fromInteger i | i <- [0..toInteger n - 1] ]
 
choose n k = fallingFactorial n k `div` factorial k
 
-- how many numbers can we get having d digits and p positions
p172 0 _ = 0
p172 d p 
    | p < 4 = d^p
    | otherwise = 
        (p172' p) +  p*(p172' (p-1)) + (choose p 2)*(p172' (p-2)) + (choose p 3)*(p172' (p-3))
    where
    p172' = p172 (d-1)
 
problem_172= (p172 10 18) * 9 `div` 10

3 Problem 173

Using up to one million tiles how many different "hollow" square laminae can be formed? Solution:

problem_173=
    let c=div (10^6) 4
        xm=floor$sqrt $fromIntegral c
        k=[div c x|x<-[1..xm]]
    in  sum k-(div (xm*(xm+1)) 2)

4 Problem 174

Counting the number of "hollow" square laminae that can form one, two, three, ... distinct arrangements.

Solution:

#include <stdio.h>
 
int main()
{
    int a=0;
    int c=1000000/4;
    int L[1000000];
    int i,x,b;
    for(i=0;i<=1000000;i++)
    {
        L[i]=0;
    }
 
    for(x=1;x<=500;x++)
    {
        int q=x*x;
        for(b=1;b<=250000;b++)
        {
            int y=q+b*x;
            if(y<=c){L[y*4]++;}
        }
    }
 
    for(i=0;i<=1000000;i++)
    {if(L[i]<=10&&L[i]>=1)
        {a++;}
    }
 
    printf( "%d\n", a);
    return 1;
}
problem_174 = main

5 Problem 175

Fractions involving the number of different ways a number can be expressed as a sum of powers of 2. Solution:

sternTree x 0=[]
sternTree x y=
    m:sternTree y n  
    where
    (m,n)=divMod x y
findRat x y
    |odd l=take (l-1) k++[last k-1,1]
    |otherwise=k
    where
    k=sternTree x y
    l=length k
p175 x y= 
    init$foldl (++) "" [a++","|
    a<-map show $reverse $filter (/=0)$findRat x y]
problems_175=p175 123456789 987654321
test=p175 13 17

6 Problem 176

Rectangular triangles that share a cathetus. Solution:

--k=47547 
--2*k+1=95095 = 5*7*11*13*19
lst=[5,7,11,13,19]
primes=[2,3,5,7,11]
problem_176 =
    product[a^b|(a,b)<-zip primes (reverse n)]
    where
    la=div (last lst+1) 2
    m=map (\x->div x 2)$init lst
    n=m++[la]

7 Problem 177

Integer angled Quadrilaterals.

Solution:

problem_177 = undefined

8 Problem 178

Step Numbers Solution:

problem_178 = undefined

9 Problem 179

Consecutive positive divisors. Solution:

#include <stdio.h>
 
#define SZ 10000000
int n[SZ + 1];
 
int main ()
{
    int i, j;
    for (i = 0; i <= SZ; i++)
        n[i] = 1;
 
    for (i = 2; i <= SZ; i++)
        for (j = i; j <= SZ; j += i)
            n[j] += 1;
 
    j = 0;
    for (i = 1; i < SZ; i++)
        if (n[i] == n[i + 1])
            j++;
 
    printf ("%d\n", j);
    return 0;
}
problem_179 = main

10 Problem 180

Rational zeros of a function of three variables. Solution:

problem_180 = undefined