Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Factorial using arrays
#1
Greetings,
Kindly assist to correct the code below.
It should find the factorial of a large number using arrays.
Thanks,
Code:
Copy      Help
 
function factorial #n
ARRAY(int) ar
ar.create(10000)
ar[0] = 1
int ar_size = 1
int x
for x 2 x<=n
ar_size = fact( x ar ar_size)
int i
for i ar_size-1 i>=0
out ar[i]



function# fact #x #ar[] #ar_size
int c = 0
int i
for i 0 i< ar_size
int p = ar[i] * x + c
ar[i] = p % 10
c = p/10


while (c)

ar[ar_size] = c%10
c = c/10
ar_size++
ret ar_size

int main()


int n

out "Enter an integer number: "
out n

out "Factorial of " is:"<<endl
factorial(n)

ret 0
Best Regards,
Philip
#2
Without arrays it could be this. With arrays don't know.

Function Factorial
Code:
Copy      Help
;/
function'long long'n

;Factorial.

;EXAMPLE
;int i
;for i 0 21 ;;factorials of bigger numbers don't fit into the long type
,;out F"{i} {Factorial(i)}"


long r(1) i
for i 2 n+1
,r*i
ret r
#3
Thank you Gintaras.
That is exactly the issue.
I need the same for large numbers.
I got this C++ code for large numbers and tried to change it to QM codes but failed.
I am missing something somewhere.

Can I use C++ code directly?
Tried but dis not run/compile.
Best Regards,
Philip
#4
Kindly advise how to pass a pointer to a an integer array to a function.
I have 2 functions as below:
The array value needs to be preserved between the recursion.
ar_size = fact(x &ar ar_size) is where I have the issue.
Code:
Copy      Help
function int'n

ARRAY(int) ar
ar.create(10000)
ar[0] = 1

int ar_size
ar_size = 1

int x

for x 2 n 1
,ar_size = fact(x &ar ar_size)
,out ar_size

int i
int previous_ar_size
previous_ar_size=ar_size-1
out previous_ar_size
for i previous_ar_size 0 -1
,out ar[i]     
[/code]
Code:
Copy      Help
 function# #x ARRAY(int)ar #ar_size
,int c = 0
,int i
,for i 0 ar_size 1
,,,int p = ar[i] * x + c
,,,ar[i] = p % 10
,,,out ar[i]
,,,c = p/10


,rep(c)
,,,ar[ar_size] = c%10
,,,c = c/10
,,,ar_size=ar_size+1
,,,out ar_size
,ret ar_size
[/code]
Best Regards,
Philip
#5
Replace
ARRAY(int)ar
with
ARRAY(int)&ar
#6
Quote:Can I use C++ code directly?
No. In QM can be used C and C#.
C example:
Macro Macro3201
Code:
Copy      Help
__Tcc c.Compile("" "Func")
int r = call(c.f 5)
out r

#ret
int Func(int i) { return i*2; }


Forum Jump:


Users browsing this thread: 1 Guest(s)