Function to multiply all digits in a number (Multiplication Persistence Test) Topic is solved
Function to multiply all digits in a number (Multiplication Persistence Test)
I would like to have the shortest function (script) to multiply all digits in a given number string. For example, in the case of 1234567, the result would be 1*2*3*4*5*6*7 (5040). I have some ideas but all of them involve splitting the string from right or left one at a time and saving each position (digit) and then multiply them all together, but this would become a very long process and seems to me not the best way.
Last edited by carno on 13 May 2019, 14:38, edited 5 times in total.
Re: Function to multiply all digits in a number string Topic is solved
Hallo,
try:
try:
Code: Select all
Number = 1234567
Result = 1
Loop, Parse, Number
Result *= A_LoopField
MsgBox,% Result
Re: Function to multiply all digits in a number string
have to consider some/many ifs.
if 7, 8, 9, 10 ?
if 7, 8, 9, 10 ?
Re: Function to multiply all digits in a number string
Code: Select all
for each, digit in StrSplit(string := 1234567), result := 1
result *= digit
MsgBox % result
10 is fine, not a digit
Re: Function to multiply all digits in a number string
Thank you all very much and my special thanks to Rohwedder! This solved my Multiplication Persistence problem using AHK.Rohwedder wrote: ↑13 May 2019, 11:49Hallo,
try:Code: Select all
Number = 1234567 Result = 1 Loop, Parse, Number Result *= A_LoopField MsgBox,% Result
Multiplication Persistence of a number refers to total number of steps required to multiply the digits of a number until the result is a single digit. The current world record is number 277777788888899 with the persistence of 11. Other numbers have equal persistence of 11 but this number is the shortest number with the biggest currently-known persistence! Thank you again for providing this beautiful function. The current search has gone as far as 233 digits and the current conjecture is that you will never beat 11. It will be a major breakthrough if someone could find a number with the Multiplication Persistence of 12.
Code: Select all
#NoEnv
#SingleInstance Force
InputBox, Number, Enter a Number, Prompt, , 250, 100, , , , , 277777788888899
If ErrorLevel = 1
ExitApp
;Number = 277777788888899
Loop {
Steps++
;MsgBox % MP(Number)
If (MP(Number) > 9) {
Number := MP(Number)
Continue
}
Else
Break
}
MsgBox % Steps
MP(Number)
{
Result = 1
Loop, Parse, Number
Result *= A_LoopField
Return Result
}
Re: Function to multiply all digits in a number (Multiplication Persistence Test)
very effective way to waste somebody else's precious time
by Oxford dictionary digit means Any of the numerals from 0 to 9.
by Oxford dictionary digit means Any of the numerals from 0 to 9.
Who is online
Users browsing this forum: RandomBoy and 176 guests