Your goal is to write a program that will print out the Laplace transform of a polynomial function with integer coefficients f(x). The Laplace transform of f(x) is defined as the integral from 0 to infinity of f(x) e^(-sx) dx.

The standard input for the polynomial function will be


If the coefficient is zero, that term is omitted. It can be taken in from any standard type of input.

This is valid input: 1+x^2-5x^4+20x^5
These aren't: 

You may output from any standard type of output, in any readable order. Trailing newline is permitted. No using built in evaluation functions.


Input: 1
Output: 1/s

Input: 3-x
Output: 3/s-1/s^2

This is code golf, so the shortest entry in bytes wins.

  • \$\begingroup\$ Will the polynomial always have integer coefficients? \$\endgroup\$ – Alex A. Aug 11 '15 at 2:36
  • \$\begingroup\$ @AlexA. Yes. Edited into the question. \$\endgroup\$ – Teoc Aug 11 '15 at 2:36
  • \$\begingroup\$ The Laplace transform of 1 is 1/s, not -1/s. \$\endgroup\$ – Dennis Aug 11 '15 at 2:43
  • \$\begingroup\$ Also, does the output have to follow the same conventions as the input, i.e., 0/s and 1/s^1 are not allowed? \$\endgroup\$ – Dennis Aug 11 '15 at 2:48
  • \$\begingroup\$ @Dennis It can be any form. \$\endgroup\$ – Teoc Aug 11 '15 at 2:49

CJam, 57 bytes


Try it online in the CJam interpreter.

| improve this answer | |

Perl, 129 bytes

Regex-based solution:

sub f{@_[0]<2?1:@_[0]*f(@_[0]-1)}$_=<>;s/(\d*)x\^?(\d*)/f($2?$2:1)*($1?$1:1).'^'.($2?$2+1:2)/eg;s/(?<!\^)(\d+)/$1.'\/s'/eg;print


sub f{@_[0]<2?1:@_[0]*f(@_[0]-1)} # define factorial subroutine
$_=<>; # read input
# do the laplace transform for all non-constant terms, without dividing by s:
s/(?<!\^)(\d+)/$1.'\/s'/eg; # divide non-exponents by s
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.