Simon Green

Posted on

# Weekly Challenge 173

Two pretty straight forward tasks this week. This is good given how challenging my day job has been recently.

You are given a positive integer, `\$n`.

Write a script to find out if the given number is Esthetic Number. An esthetic number is a positive integer where every adjacent digit differs from its neighbor by 1.

## My solution

One of the main difference between Python and Perl (other than the syntax) is variables are typed in Python where they aren't in vanilla Perl (I'm excluding Moose like variables here). This always means there is a little extra work required in Python to get the right solution.

For this challenge, I simply loop from the first character to the second last character. I compare the absolute difference (using the `abs` function) between that character and the next one. If the difference is not 1, we know the number is not esthetic, and can print `0` and exit the loop. If we reach the end of the loop, we can print `1`.

## Examples

``````\$ ./ch-1.py 5456
1

\$ ./ch-1.py 120
0
``````

Write a script to generate first 10 members of Sylvester's sequence.

## My solution

The big issue with this challenge is that the numbers involved are very large, and most importantly > 231-1. Python will automatically convert an integer into a long and play along nicely.

For the Perl solution, using Math::BigInt will achieve the same outcome.

In my code, I seed the `solutions` list (array in Perl) with S0 of `2`. I then have a loop that iterates until there are 10 values in the list. For each iteration, I take the product of the current values, and add one.

In the Python code, I use `math`s prod function, while I list `List::Util`s product function in my Perl code.

## Examples

``````\$ ./ch-2.py
2
3
7
43
1807
3263443
10650056950807
113423713055421844361000443
12864938683278671740537145998360961546653259485195807
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
``````