Isnochys Math Problem #4.1 [HSBI]

in STEMGeeks4 months ago

Last week we faced the issues of Palindromes.
This week we are going to solve it.
With Python.

First, lets have a look at the obvious solution:
We need to find a Palindrome. Forward is the same as backwards:

str(x*y) == str(x*y)[::-1]

This line takes the product(xy) makes a string out of it, str(xy), inverts that string, [::-1], and compares it to the original string.
"[a:b:c]" is so much fun to work with and every time I get very confused and will google it:))

Next one is to find the values for x and y:

for x in range(9000,10000):
   for y in range(x,10000):

We just try numbers above 9000, all the other are not worth to test.
Now mix everything together:

maxpal = 0
for x in range(9000,10000):
   for y in range(x,10000):
       p = x * y
       if str(p) == str(p)[::-1] and p > maxpal:
           maxpal = p
print(maxpal)

We run it and get the answer:
99000099

There you go!

But now for the fun one:

max([x*y for x in range(9000,10000) for y in range(x,10000) if str(x*y) == str(x*y)[::-1]])

What is this?
The maximum out of the list:

[x*y for x in range(9000,10000) for y in range(x,10000) if str(x*y) == str(x*y)[::-1]]

The list are items made of the product x*y
and the rest are the same for loops and if statements as described above to filter it.
Result is the same.

This week, there is no homework for you, just post a comment or question and get some BEER

Don't forget to witness vote for me!:)

Posted with STEMGeeks

Sort:  

Great Post!

!1UP

great comment
!BEER

 4 months ago Reveal Comment
Don-1UP-Cheers-Cartel-250px.png

You have received a 1UP from @luizeba!

The following @oneup-cartel family members will soon upvote your post:
@bee-curator, @ccc-curator, @stem-curator, @vyb-curator, @pob-curator
And they will bring !PIZZA 🍕

Learn more about our delegation service to earn daily rewards. Join the family on Discord.

PIZZA!

PIZZA Holders sent $PIZZA tips in this post's comments:
@curation-cartel(12/20) tipped @isnochys (x1)

You can now send $PIZZA tips in Discord via tip.cc!

Bookmarked :)

Hatte noch keine Zeit, das letzte Rätsel auszutesten..

Dann erstmal in Ruhe das !BEER genießen.

Thank you for your witness vote!
Have a !BEER on me!
To Opt-Out of my witness beer program just comment !STOP below

 4 months ago Reveal Comment
 4 months ago Reveal Comment

Congratulations @isnochys! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You distributed more than 57000 upvotes.
Your next target is to reach 58000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Check out the last post from @hivebuzz:

Hive Power Up Month - Feedback from April day 6
NFT for Peace - Feedback and new city
Support the HiveBuzz project. Vote for our proposal!