The problem I'm trying to solve is the simple but lovely euler 62.
The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
Find the smallest cube for which exactly five permutations of its digits are cube.
A bit of fun coding after a statistics midterm last night and the solution should be in the bag. Except it isn't, I am doing something wrong somehow and I can't figure out what! Hopefully someone a bit better than me can shed some light whether my proposed solution is wrong or I just suck at Haskell.
- Generate cubes up to 10,000
- Every cube is a pair of a digit-ordered string n^3 and n, _for instance ("279",9)_
- Order cubes by the string number presentation
- Group together all cubes with the same n^3
- Pick out groups with the size of 5
- Sort by n
- Pick the smallest number
Should work in principle right? So why doesn't the website accept my answer (5027)? My guess is I'm doing something wrong in the sorting and grouping department and I was hoping someone with a bit more knowledge of Haskell could point out where I'm being stupid.
import Data.Listcubes::(Num a) => a -> [(String, a)]cubes 1 = [(show 1, 1)]cubes n = (sort$show(n^3), n):(cubes $ n-1)sortStrNum (s1, n1) (s2, n2)| length s1 == length s2 = compare s1 s2| otherwise = compare (length s1) (length s2)permuted_cubes n =groupBy (\a b -> fst a == fst b) $ sortBy sortStrNum $ cubes nfives n =filter (\xs -> length xs == 5) $ permuted_cubes ncomparing p x y = compare (p x) (p y)smallest =head $ sortBy (comparing snd) $ head $ fives 100000
The whole thing looks kind of alright to me, no matter how much I poke around it doesn't seem like something is misbehaving ... but it still is.
- Collatz, Haskell and Memoization (swizec.com)
- Learning me a Haskell (swizec.com)
- Converting Haskell Polymorphic Cosine function to F# (stackoverflow.com)
- Mutable, (possibly parallel) Haskell code and performance tuning (stackoverflow.com)
- Will this Haskell code take too much memory? (stackoverflow.com)
- Exceptions in Haskell (stackoverflow.com)
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Senior Mindset Book
Get promoted, earn a bigger salary, work for top companiesLearn more
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️