Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
factorial powers program problems
Author Message
Chimaera

Posted: Sun Nov 30, 2003 5:46 pm   Post subject: 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

 code: 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

Posted: Sun Nov 30, 2003 10:23 pm   Post subject: (No subject)

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:
 code: 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

Posted: Mon Dec 01, 2003 1:56 pm   Post subject: (No subject)

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!

Posted: Mon Dec 01, 2003 2:36 pm   Post subject: (No subject)

strint(var:string) -> string to integer function
intstr(var:int) -> integer to string funtion
Andy

Posted: Mon Dec 01, 2003 4:26 pm   Post subject: (No subject)

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

Posted: Mon Dec 01, 2003 5:18 pm   Post subject: (No subject)

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<cnt2-cnt1;cnt++){ num2=(num2*2)%10;}
for (j=1;j<=num;j++) {num1=j; while(num1%2==0) num1/=2; while(num1%5==0) num1/=5; num2=(num2*num1%10)%10; }
cout<<"The right most non-zero digit is "<<num2<<" and it is followed by "<<cnt1<<"zeros!!"<<endl;
Andy

Posted: Mon Dec 01, 2003 6:25 pm   Post subject: (No subject)

and i'll translate that

 code: num1:=num loop exit when num1<=0 num1:=num1/5 cnt+=num1 end loop num1:=num loop exit when num1<=0 num1:=num1 div 2 cnt2+=num1 end loop num2:=1 for i:1..cnt2-cnt1-1 num2:=(num2*2) mod 10 end for for i:1..num num1:=j loop exit when num1 mod 2 not=0 num1:=num1 div 2 end loop loop exit when num1 mod 5 not=0 num1:=num1 div 5 end loop num2:= (num2*num1 mod 10) mod 10 end for put "The right most non-zero digit is ",num2," and it is followed by ",cnt1,"zeros!!";

wow... so much more complicated in turing
bugzpodder

Posted: Mon Dec 01, 2003 6:40 pm   Post subject: (No subject)

copycat!!

Andy

Posted: Mon Dec 01, 2003 6:41 pm   Post subject: (No subject)

jack, this is in the turing help section... i dont think the kids who ask for help know c++... thats why i said i translated yours?
bugzpodder

Posted: Mon Dec 01, 2003 8:33 pm   Post subject: (No subject)

sheesh, stop spamming already... geez people these days? how many bits do you need man
do something useful and explain your program if you think you are so useful?
I purposely did NOT write Turing code! the idea is to learn from the code, NOT COPY AND PASTE~

do you think that I cant program in turing?
Mazer

Posted: Mon Dec 01, 2003 8:45 pm   Post subject: (No subject)

bugzpodder wrote:
do you think that I cant program in turing?

now where would dodge get a crazy idea like that
bugzpodder wrote:
i cant program in turing anymore
Andy

Posted: Mon Dec 01, 2003 8:47 pm   Post subject: (No subject)

exactly... u said it not me
Chimaera

Posted: Tue Dec 02, 2003 9:45 pm   Post subject: (No subject)

ya this question was from a 1995 programming competition and my teacher said it was a very common problem for programmers because often they have to deal with numbers that're out of range for their integers, reals etc. etc. So they have to devise a solution which is sort of similar to what I'm trying to do here...
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 13 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: