
-----------------------------------
Chimaera
Sun Nov 30, 2003 5:46 pm

factorial powers program problems
-----------------------------------
I'm having problems with finding the correct number of zeros from the furthest right non zero digit and also turing doesn't allow me to save the value of the factorial of the number once I go past 12, is there any way I can solve these problems with the program? Thanks


var num : int
var fnum : string
var znum : int

get num
for i : 1 .. num - 1
    num := num * i
end for
fnum := intstr (num)
put fnum

if index (fnum, "0") = 0 then
    put "The right-most non-zero digit of ", fnum, "! is ", fnum (*), " and it is followed by no zeros"
elsif index (fnum, "0") not= 0 and index (fnum, "00") = 0 then
    put "The right-most non-zero digit of ", fnum, "! is ", fnum (*-1), " and it is followed by 1 zero"
elsif index (fnum, "00") not= 0 then
znum := length (fnum (index (fnum, "00") .. *))
    put "The right-most non-zero digit of ", fnum, "! is ", fnum (*-(length (fnum(index(fnum, "00")..*)))), " and it is followed by ",znum , " zeros."
end if



-----------------------------------
Mazer
Sun Nov 30, 2003 10:23 pm


-----------------------------------
i don't really understand what the problem is with the factorials, but i think i can help with the nonzero number thing.

from what i see what you're trying to do is check if there is one zero, then if there are two zeroes, and so on. the problem with that is that it's really inefficient and if you had a number like 10 trillion (which is too big but oh well) things would get ugly. you should make a function that:
-takes an int
-converts it to a string
-then goes through the string backwards until it doesn't find a zero
-converts that part of the string back to an integer
-return the integer

this should do it:

function rNonZero (num : int) : int
    var snum := intstr (num)
    for decreasing i : length (snum) .. 1
        if snum (i) != "0" then
            result strint (snum(i))
        end if
    end for
    result -1 % this will tell you that you have no zeroes in the number
end rNonZero


-----------------------------------
Chimaera
Mon Dec 01, 2003 1:56 pm


-----------------------------------
the problem with the factorials is that the numbers is causing an integer overflow, I need a way to convert the integer to string while saving the value of the num!

-----------------------------------
Blade
Mon Dec 01, 2003 2:36 pm


-----------------------------------
strint(var:string) -> string to integer function
intstr(var:int) -> integer to string funtion

-----------------------------------
Andy
Mon Dec 01, 2003 4:26 pm


-----------------------------------
we had a problem similar to this with the last dwite. i would do what blade said and use strint. and when u are multiplying a new number on, use the steps of how you would multiply by hand and manipulate the string character by character

-----------------------------------
bugzpodder
Mon Dec 01, 2003 5:18 pm


-----------------------------------
i cant program in turing anymore so i'll do it straight C++

num1=num;
while(num1>0) {num1/=5; cnt1+=num1;}
num1=num;
while(num1>0) {num1/=2; cnt2+=num1;}
num2=1;
for (i=0;i