الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سؤال حول Recursion

بدأه fouad2008 في 8 يوليو 2009 · 8 رد · 1,362 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع
/*
* Fonction recursive simple.
*/
unsigned long factoriel (int n)
{

  if (n == 1 || n == 0) 
	  return 1;

  return n * factoriel (n - 1);
}

my question is : for example if i invok factoriel(4) it gives me 24 (good) ,but when changed ( return 1 ) to ( return 2) after the first condition it gives me 48 . can some body explain this behave !! i personally and after alot of reading about recursion i didn't find any thing.

excuse me for english //

تم تعديل هذه المشاركة بواسطة fouad2008 في 8 يوليو 2009 في 16:47

#2

welcome...

Typically, there is no need to return 2 and of course it's wrong to do so, because this point must be the last point before the factorial function is being returned. Don't forget that n will be decreased and at sometime will be 1. And behind the scene it will do the last multiplication process.

It's the habit of the recursion process-- called by itself...15_3_41.gif

That is the reason for why you've always got 48 when you pass in 4 as a factorial number or any... Because 24 * 2 = 48... at sometime it will come back again to check for 1 that is true then doing the last call to itself...

Sorry I think so...

Bye...

الله أعلم...

للإدارة:

آسفة بخصوص الكتابة باللغة الإنجليزية

تم تعديل هذه المشاركة بواسطة ♥رغـد♥ في 8 يوليو 2009 في 19:27

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

#3

thanks Ragad for repy

but still can't figure out whats happend behind the scene in this line (return 1) , what is the true meaning of 1 .

thanks again

#4
اقتباس
what is the true meaning of 1 .
اقتباس
if (n == 1 || n == 0)

(n == 1) it returns 1, which equals n, so that it will multiple by the previous number (1 * 2 * 3....)

(n == 0) it returns 1, because it is just a rule

bye

اقتباس
للإدارة:

آسفة بخصوص الكتابة باللغة الإنجليزية

تم تعديل هذه المشاركة بواسطة MoHammaD_93 في 9 يوليو 2009 في 00:02

SAFETY TIP : Always delete what you new, and free what you malloc, never mix new with free or malloc with delete.

#5
fouad2008 كتب:
thanks Raghad for repy

but still can't figure out whats happend behind the scene in this line (return 1) , what is the true meaning of 1 .

thanks again

Well!!

Okay, you have to believe that the first process of this is just CALLING functions not more. Ok? :happy:

Until the programm is reaching and getting within the body of the last called function.

In this example it is the last factorial(1)...at this time the condition is true then it will return 2 to the immediate previous caller and the progress will be continued backwards to the upper caller again and again and again...

Ok...

I've drawn the following picture to grasp the operation...

post-140583-1247092003_thumb.png

So you may fiqure out what if the returned value was 1 instead of 2!!

It's so easy to grasp...please just read my note...

Thank you...

bye...

و آسفة مرة تانية للإدارة

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

#6

Welcome again...

post-140583-1247183050_thumb.png

LEFT TO RIGHT

Yes indeed, there is no magic here...the above figure picture describes the mathematical fact of the factorial operation but programmatically.

The factorial is the product of all integer numbers from 1 up to a specified number...

For instance...the factorial of 4 (mathematically) is:

mimetex.cgi?%204!%5C,=%5C,4%5C,%5Ctimes%

Yeah! so according to this! the number 1 has to be involved in the factorial process somehow and the innermost returned value must be 1.

Now! may you read the previous line(the example) from right to left and the previous figure picture from down to up to grasp this process clearly...

So for this math fact and this rule the returned value must be 1...

Sorry, I know that you know! but now the process is clear for which why it should be 1 instead of 2 or of any other number because the last|innermost multiplication operation has to be by 1.

bye...

آسفة مرة تالتة للإدارة

تم تعديل هذه المشاركة بواسطة ♥رغـد♥ في 10 يوليو 2009 في 03:15

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

#7

للاخوان والاخوات ..

من قواعد الكتابة في المنتدى استخدام اللغة العربية الفصحى لانها مفهومة للجميع .

ربما يستثنى من تعذر عليه الكتابة بالعربية لسبب ما , لكن من هو خلاف ذلك فلا اعتقد انه يمتلك اي مبرر .

المقصد انها مفهومة للشخص كاتب السؤال , ومفهومة لاي شخص اخر حاول البحث بعد مدة عن نفس السؤال لكي لا يضطر إلى كتابة السؤال مرة اخرى ...

llback.jpg

اشهد ان لا إله إلا الله وان محمدا ً رسول الله

#8

معك حق!! آسفة

نسخة عربية للموضوع:

الأخ الكريم فؤاد صاحب الموضوع بيسأل متعجباً عن "لماذا عندما قام بتغيير القيمة المسترجعة من 1 إلى 2 و الموجودة داخل جسم جملة الشرط if قامت الدالة باسترجاع قيمة غير متوقعة للعملية الحقيقية لمفهوم factorial الحسابية"

و هي الشيفرة التالية:

  1.  
  2. /*
  3. * Fonction recursive simple.
  4. */
  5. unsigned long factoriel (int n)
  6. {
  7.  
  8. if (n == 1 || n == 0)
  9. return 1;
  10.  
  11. return n * factoriel (n - 1);
  12. }
  13.  

حسناً!

من الحقيقة الرياضية أن factorial هي عملية إيجاد قيمة مضروب العدد n في نفسه و نزولاً إلى العدد واحد. مسلاً كالتالي للعدد اربعة

:P ...

تم تعديل هذه المشاركة بواسطة ♥رغـد♥ في 11 يوليو 2009 في 02:27

[وسط]

♥ Countess ♥

57899411.gif

♥

[/وسط]

#9

thank you very much Ragad, now its clear . you make Recursion so easy . i'd like to see tutorials made by you if you already made one :happy:

thanks again

i appologize for english ( thats because i didn't install the arabic font yet ) .

مواضيع مشابهة