Re: How to calculate/process this one?
Re: How to calculate/process this one?
- Subject: Re: How to calculate/process this one?
- From: email@hidden (Michael Sullivan)
- Date: Sat, 27 Apr 2002 15:17:50 -0400
- Organization: Society for the Incurably Pompous
>
On 4/27/02 9:42 AM, "Arthur J Knapp" <email@hidden> wrote:
>
>
>> Let's say I have a random number like 11050. Now I want to add each pair
>
>> of consecutive digits to give the next line (so 11050 becomes 2155). I
>
>> want to repeat this step until I have a two-digit number.
>
>
> I don't understand, how does 11050 become 2155 ???
>
>
He's adding every single (overlapping) adjacent pair.
>
1 + 1 = 2 -- digits 1 and 2
>
1 + 0 = 1 -- digits 2 and 3
>
0 + 5 = 5
>
5 + 0 = 5
>
The only reason he number of digits is reduced is because of all those 0 and
>
1 digits keeps the individual sums less than 10. if he's started out with
>
99599
>
9 + 9 = 18
>
9 + 5 = 14
>
5 + 9 = 14
>
9 + 5 = 18
>
we now have an 8-digit number 18141418.
>
The next number would be 9955559. Then you'd get 181410101014. Then
>
9955111115. Then 181410622226. I don't think it's ever going to reduce to 1
>
digit, although I wouldn't like to have to prove that.
>
The whole idea that you could reduce the length while trying to concatenate
>
_additions_ which increase the numbers of digits every time he sum is
>
greater than 10 is - well, I'm not going to use the word that comes to mind.
See, now I was thinking that you'd define the sum of the 10^4 and 10^3
digits to be the value of the 10^3 place and likewise down the chain
until the end, rather than using simple concatenation, so 99599 would
become 19558, which would then become 11513, then 2664. So this way,
all numbers would eventually end up between 0 and 9 with repeated
operations.
something like this:
set k to {deconstruct(99599), deconstructToDeath(99599)}
--> {19558, 8}
on deconstructToDeath(n)
(* decimate? dessicate? Why do I find this so very amusing?
* Do I need psychological help?
*)
try
if n div 10 < 1 then
return n
end if
on error
error "deconstructToDeath(n): " & "Parameter is not a number."
end try
repeat until (n div 10) < 1
set n to deconstruct(n)
end repeat
return n
end deconstructToDeath
on deconstruct(n)
try
if n is not equal to n div 1 then
error "deconstruct(n) only works with integer values."
end if
on error
error "deconstruct(n): " & "Parameter is not a number."
end try
if n div 10 < 1 then
return n
end if
set ndigits to getDigits(n)
set newN to 0
repeat with i from (count ndigits) to 2 by -1
set exp to (count ndigits) - i
set temp to (item i of ndigits) + ((item (i - 1)) of ndigits)
set newN to newN + temp * (10 ^ exp)
end repeat
return newN
end deconstruct
on getDigits(n)
try
if n is not equal to n div 1 then
error "getDigits(n) only works with integer values."
end if
on error
error "getDigits(n): " & "Parameter is not a number."
end try
set theList to {}
set exp to 1
repeat until (10 ^ (exp - 1)) > n
set beginning of theList to (n mod (10^exp)) div (10^(exp - 1))
set exp to exp + 1
end repeat
return theList
end getDigits
Okay that was sort of amusing. The number theory geek in me wants to
think about whether what number deconstructToDeath(n) returns says
anything interesting about n...
But so far, I'm not coming up with anything good. Nor any useful
purpose to which this script may be put.
What was the genesis of the original problem anyway? Just a game to
play with code, or is there actually a real world problem that this
helps solve? I can imagine the getDigits portion of this routine being
useful, although far more so if it were modified to handle the mod 1
portion as well.
Michael
--
Michael Sullivan
Business Card Express of CT Thermographers to the Trade
Cheshire, CT email@hidden
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.