Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Digit Isolation
#1
My first challenge to you is to write a program that'll split a whole number of up to 15 digits into single digits that hold each place value. For example:

5918525 would return 5, 9, 1, 8, 5, 2, and 5.

However, you may only achieve this by using mathematical functions! Good luck! I'll be on my ol' DOS computer trying to do it too Wink.
LinkMaster Sab
Reply
#2
Code:
DECLARE SUB isolate (a&)
a& = 987654321
PRINT "isolating "; a&
isolate a&

SUB isolate (a&)
IF a& = 0 THEN EXIT SUB
b& = a& MOD 10
a& = a& \ 10
isolate a&
PRINT b&
END SUB
Antoni
Reply
#3
Antoni Gual's doesn't work when given a zero, better it's still better than mine.

I always thought you'd have to use an array/stack to start at the one's place and still print them in order - nice work!

Well, anyway, here's mine:

Code:
DEFLNG A-Z

INPUT "Enter a non-negative integer: ", n

divisor = 1000000000  ' start at the billions place
                      ' LONGs cannot have any more digits than this

PRINT "Here are the digits, in order:";

' This flag will be used to clip off leading zeros:
havePrintedANonZeroDigit = 0

DO UNTIL divisor = 1

   digit% = (n \ divisor) MOD 10
   divisor = divisor \ 10

   IF (digit% <> 0) OR havePrintedANonZeroDigit THEN
      PRINT digit%; ",";
      havePrintedANonZeroDigit = -1
   END IF

LOOP

' This ensures that for n = 0, a zero will be printed:
PRINT n MOD 10  ' Print the last digit (in the one's place)

END
Reply
#4
Yea, I realized that the algorithms I wrote won't work with 15 digits... so 10 has to work Wink. Well, here's my code... it uses MOD and such, probably not as good as you guys though...

Code:
DECLARE SUB isolate (num)
DIM SHARED dig(10)
COMMON SHARED length
CLS
DO
INPUT "What number (1 - 9999999999) to isolate"; num
LOOP UNTIL num > 0 AND num < 9999999999
isolate (num)
FOR x = 1 TO 10
IF length >= x THEN PRINT dig(x);
NEXT x

SUB isolate (num)
FOR x = 1 TO 10
IF num < (10 ^ x) THEN length = x: GOTO lengthfound
NEXT x
lengthfound:
dig(1) = INT(num / (10 ^ (length - 1)))
FOR x = 2 TO length
dig(x) = num MOD 10 ^ (length - (x - 1))
dig(x) = INT(dig(x) / (10 ^ (length - x)))
NEXT x
END SUB
LinkMaster Sab
Reply
#5
Reverse order...

Code:
CLS
num1& = 5918525
DO
num2& = num1& \ 10
PRINT num1& - num2& * 10
IF num2& = 0 THEN EXIT DO
num1& = num2&
LOOP
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#6
You guys and your fancy short codes which do all that mine do yet are much simpler :wink:.
LinkMaster Sab
Reply
#7
Here ya go. I see this is basically Antoni's algorithm, but I'll post it anyways since it's a bit shorter and allows for input of 0.

Code:
DECLARE SUB split (n&)
INPUT n&
IF n& = 0 THEN PRINT 0 ELSE split n&

SUB split (n&)
     IF n& THEN split n& \ 10 ELSE EXIT SUB
     PRINT n& MOD 10
END SUB

*peace*

Meg.
Reply
#8
That's what i meant :oops:
Your code is the best, as it happens always when you decide to show up... :lol:
Antoni
Reply
#9
title reiterated.

*peace*

Meg.
Reply
#10
I need to learn how to do recursion effectively Big Grin
LinkMaster Sab
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)