Skip to content

Instantly share code, notes, and snippets.

@AndrewTweddle
AndrewTweddle / ClosedFormSumsOfKthPowers.ps1
Last active December 2, 2015 11:29
Use SymPy to convert a summation from 1 to n over an arbitrary polynomial in a variable i to a closed form polynomial in variable n
0..10 | % { python GuessClosedForm.py "i ** $_"; Write-Host }
@AndrewTweddle
AndrewTweddle / JHMWorksheet.scala
Last active August 29, 2015 13:56
A Scala worksheet to compare the performance of the various solvers written to solve the Johnny Hates Math problem found at http://www.spoj.com/problems/ANARC07J/
import jhm.JHMSolver
import jhm.SolverUsingFlatMap
import jhm.SolverUsingFor
import jhm.SolverUsingFoldLeft
object JHMWorksheet {
implicit val repetitions: Int = 20 //> repetitions : Int = 20
// Adapted from code found on: http://stackoverflow.com/questions/9160001/how-to-profile-methods-in-scala ...
// UPDATE: see the following article for why this is a bad idea... http://shipilev.net/blog/2014/nanotrusting-nanotime/
@AndrewTweddle
AndrewTweddle / SolverUsingFoldLeft.scala
Last active August 29, 2015 13:56
A solver using Scala's foldLeft to solve the Johnny Hates Math problem found at http://www.spoj.com/problems/ANARC07J/ (NB: this is to find all solutions to the problem, not to satisfy the SPOJ IO specification)
package jhm
case class SolverUsingFoldLeft( digits: String, total: Int, maxDigitsPerNumber: Int = 5
) extends JHMSolver(digits, total, maxDigitsPerNumber) {
case class PartialSolution( val partialStr: String, val partialNumber: Int,
val subTotal: Int, val numbersInReverse: List[Int] )
{
def runningSubTotal = subTotal + partialNumber
def isInitialSolution = partialStr == ""
@AndrewTweddle
AndrewTweddle / SolverUsingFor.scala
Last active August 29, 2015 13:56
A solver using Scala's for-yield syntax to solve the Johnny Hates Math problem found at http://www.spoj.com/problems/ANARC07J/ (NB: this is to find all solutions to the problem, not to satisfy the SPOJ IO specification)
package jhm
case class SolverUsingFor( digits: String, total: Int, maxDigitsPerNumber: Int = 5
) extends JHMSolver(digits, total, maxDigitsPerNumber) {
override def solve(digits: String, total: Int, maxDigitsPerNumber: Int = 5): List[List[Int]] = for {
leftCount <- (1 to maxDigitsPerNumber).toList
if leftCount <= digits.length
(leftStr, rightStr) = digits.splitAt(leftCount)
if (leftCount == 1 || leftStr(0) != '0') // No numbers may start with '0' (except zero)
@AndrewTweddle
AndrewTweddle / SolverUsingFlatMap.scala
Last active August 29, 2015 13:56
A solver using Scala's flatmap to solve the Johnny Hates Math problem found at http://www.spoj.com/problems/ANARC07J/ (NB: this is to find all solutions to the problem, not to satisfy the SPOJ IO specification)
package jhm
case class SolverUsingFlatMap( digits: String, total: Int, maxDigitsPerNumber: Int = 5
) extends JHMSolver(digits, total, maxDigitsPerNumber) {
override def solve(digits: String, total: Int, maxDigitsPerNumber: Int = 5): List[List[Int]] = {
def getSolutionsForNextNumber(leftCount: Int): List[List[Int]] = {
val (leftStr, rightStr) = digits.splitAt(leftCount)
if (leftCount > 1 && leftStr(0) == '0') List.empty // Disallow numbers starting with zero
@AndrewTweddle
AndrewTweddle / JHMSolver.scala
Last active August 29, 2015 13:56
A base class to solve the Johnny Hates Math problem found at http://www.spoj.com/problems/ANARC07J/ (NB: this is to find all solutions to the problem, not to satisfy the SPOJ IO specification)
package jhm
abstract class JHMSolver(digits: String, total: Int, maxDigitsPerNumber: Int = 5)
{
def solve(digits: String, total: Int, maxDigitsPerNumber: Int = 5): List[List[Int]]
lazy val solutions = solve(digits, total, maxDigitsPerNumber)
def getSolutionsAsString(): String = solutions match {
case Nil => "IMPOSSIBLE"
@AndrewTweddle
AndrewTweddle / JHM.ps1
Created February 20, 2014 20:03
A Powershell solution to the Johnny Hates Math problem on http://www.spoj.com/problems/ANARC07J/
function calc {
param (
[String] $str,
[int] $target
)
foreach ($len in 1..5 ) {
$firstStr = $str.SubString(0, $len)
if ($firstStr.Length -lt $len) {
break
}