In this example, you will see: private static function, recursive fibonacci and dynamic fibonacci algorithm.

NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.

example_3.hx:

/**
 * @author: Alejandro G. Carlstein R. M.
 */

class Example_3 {

	private static var fib_array : Array<Int> = [1, 1];

	static function main(){
		trace('main()');
		trace('Recursive Fibonacci (value: 5): ' + recursive_fib(5));
		trace('Dynamic Fibonacci (value: 5): ' + dynamic_fib(5));
		print_array();
	}

	// Recursive Fibonacci
	private static function recursive_fib(value : Int) : Int {
		if (value < 2)
			return 1;
		return recursive_fib(value - 1) + recursive_fib(value - 2);
	}

	// Dynamic Fibonacci
	private static function dynamic_fib(value : Int) : Int {
		if (value < 2)
			return 1;
		if (fib_array[value] == 0)
			fib_array[value] = dynamic_fib(value - 1) + dynamic_fib(value - 2);
		return fib_array[value];
	}

	private static function print_array() : Void{
		var iter = fib_array.iterator();
		while (iter.hasNext()){
			trace('Array Value: ' + iter.next());
		}
	}

}

If you encounter any problems or errors, please let me know by providing an example of the code, input, output, and an explanation. Thanks.

© 2010, Alejandro G. Carlstein Ramos Mejia. All rights reserved.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

*

Click to Insert Smiley

SmileBig SmileGrinLaughFrownBig FrownCryNeutralWinkKissRazzChicCoolAngryReally AngryConfusedQuestionThinkingPainShockYesNoLOLSillyBeautyLashesCuteShyBlushKissedIn LoveDroolGiggleSnickerHeh!SmirkWiltWeepIDKStruggleSide FrownDazedHypnotizedSweatEek!Roll EyesSarcasmDisdainSmugMoney MouthFoot in MouthShut MouthQuietShameBeat UpMeanEvil GrinGrit TeethShoutPissed OffReally PissedMad RazzDrunken RazzSickYawnSleepyDanceClapJumpHandshakeHigh FiveHug LeftHug RightKiss BlowKissingByeGo AwayCall MeOn the PhoneSecretMeetingWavingStopTime OutTalk to the HandLoserLyingDOH!Fingers CrossedWaitingSuspenseTremblePrayWorshipStarvingEatVictoryCurseAlienAngelClownCowboyCyclopsDevilDoctorFemale FighterMale FighterMohawkMusicNerdPartyPirateSkywalkerSnowmanSoldierVampireZombie KillerGhostSkeletonBunnyCatCat 2ChickChickenChicken 2CowCow 2DogDog 2DuckGoatHippoKoalaLionMonkeyMonkey 2MousePandaPigPig 2SheepSheep 2ReindeerSnailTigerTurtleBeerDrinkLiquorCoffeeCakePizzaWatermelonBowlPlateCanFemaleMaleHeartBroken HeartRoseDead RosePeaceYin YangUS FlagMoonStarSunCloudyRainThunderUmbrellaRainbowMusic NoteAirplaneCarIslandAnnouncebrbMailCellPhoneCameraFilmTVClockLampSearchCoinsComputerConsolePresentSoccerCloverPumpkinBombHammerKnifeHandcuffsPillPoopCigarette