<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
On 02/27/2011 03:08 AM, Mike Miller wrote:
<blockquote
cite="mid:alpine.DEB.2.00.1102262148190.26842@taxa.psych.umn.edu"
type="cite">On Sat, 26 Feb 2011, Erik Mitchell wrote:
<br>
<br>
<blockquote type="cite">One thing I'd suggest looking into is
Project Euler. It provides a great set of computation problems
to work through, and you can do it in any language you like.
Having real programming problems to work on helps because you're
not paying as much attention to the language as you are figuring
out how to solve the problem at hand.
<br>
<br>
<a class="moz-txt-link-freetext" href="http://projecteuler.net/">http://projecteuler.net/</a>
<br>
</blockquote>
<br>
<br>
Fun. Never heard of it. I'll do some using GNU commands from the
bash prompt. Let me know if you have ways of making these better
(e.g., shorter or faster).
<br>
<br>
<br>
Here's my effort on Problem #1, using only GNU utils on the Bash
command line:
<br>
<br>
seq 999 | awk '$1%5==0 || $1%3==0 {s+=$1}{print s}' | tail -1
<br>
<br>
I like to do lots of math/stats in GNU R or GNU Octave, both great
programs, but I also do a lot of things on the Bash command line
because it is very quick and efficient, once you've learned the
tricks. It took me many years to get good enough, a little at a
time. I'm sure one good course would do it, but I never had one.
<br>
<br>
<br>
Here's my GNU/bash answer to Problem #2:
<br>
<br>
( x=1 ; y=1 ; while [ $y -lt 4000000 ] ; do z=$y ; let y=$x+$z ;
x=$z ; echo $z ; done ) | awk '$1%2==0 {s+=$1}{print s}' | tail -1
<br>
<br>
<br>
Here's my answer to Problem #3:
<br>
<br>
( x=600851475143 ; max_fac=10000 ; while [ $x -gt 1 ] ; do i=$(seq
2 $max_fac | awk -v x=$x 'x%$1==0' | head -1) ; echo $i ; let
x=$x/$i ; done ) | tail -1
<br>
<br>
I did it first with a larger value of max_fac, but that slows it
way down.
<br>
<br>
This will print the prime factors of the number "x" in order, as
long as all of them are less than max_fac, but keep max_fac small
because larger max_fac slows it down:
<br>
<br>
x=600851475143 ; max_fac=10000 ; while [ $x -gt 1 ] ; do i=$(seq 2
$max_fac | awk -v x=$x 'x%$1==0' | head -1) ; echo $i ; let
x=$x/$i ; done
<br>
<br>
With a double while-loop and no max_fac, it is slow on the larger
factors:
<br>
<br>
x=600851475143 ; while [ $x -gt 1 ] ; do f=2 ; while [ $(echo $f |
awk -v x=$x '{print x%$1}') -gt 0 ] ; do let f=$f+1 ; done ; echo
$f ; let x=$x/$f ; done
<br>
<br>
<br>
Another GNU/bash answer, this for Problem #4:
<br>
<br>
( for i in $(seq 100 999) ; do for j in $(seq 100 $i) ; do let
k=$i*$j ; echo $k ; echo $k | rev ; done ; done ) | uniq -c | awk
'$1 > 1' | cut -c9- | sort -n | tail -1
<br>
<br>
It isn't very efficient, though. It took 13 minutes on my
machine. I could have taken a shortcut by guessing that the
answer would involve larger numbers. This gives the same answer
in 23 seconds:
<br>
<br>
( for i in $(seq 901 999) ; do for j in $(seq 901 $i) ; do let
k=$i*$j ; echo $k ; echo $k | rev ; done ; done ) | uniq -c | awk
'$1 > 1' | cut -c9- | sort -n | tail -1
<br>
<br>
I did use "rev" which could be considered cheating. It can be
written in pure internal bash but that is awkward and a bit long.
<br>
<br>
<br>
This is my answer for Problem #5:
<br>
<br>
let x=2*3*2*5*7*2*3*11*13*2*17*19 ; echo $x
<br>
<br>
It's just too easy to do it that way. If they wanted something
bigger, maybe I'd program the algorithm.
<br>
<br>
<br>
Problem #6:
<br>
<br>
seq 100 | awk '{s+=$1}{s2+=$1^2}{print s^2-s2}' | tail -1
<br>
<br>
<br>
Must sleep...
<br>
<br>
<br>
Mike
<br>
_______________________________________________
<br>
TCLUG Mailing List - Minneapolis/St. Paul, Minnesota
<br>
<a class="moz-txt-link-abbreviated" href="mailto:tclug-list@mn-linux.org">tclug-list@mn-linux.org</a>
<br>
<a class="moz-txt-link-freetext" href="http://mailman.mn-linux.org/mailman/listinfo/tclug-list">http://mailman.mn-linux.org/mailman/listinfo/tclug-list</a>
<br>
</blockquote>
Addictive isn't it?<br>
<br>
<div class="moz-signature">-- <br>
Patrick "Finn" Robins
<br>
<em>Be who you are and say what you feel, because those who mind
don't matter and those who matter don't mind.<br>
- Dr. Seuss</em><br>
<img alt="" src="cid:part1.05090005.02030500@gmail.com"
height="20" width="350"></div>
</body>
</html>