What is Egyptian fraction problem? Trick to print 1–100 numbers without any loop, function or recursion.
This article contains information about what is Egyptian fraction problem and how to solve it in C++ language. Also, a simple trick to print numbers from 1–100 without any control statement or function. Let’s get started.
What is Egyptian fraction problem?
An Egyptian fraction is a finite sum of distinct unit fractions in such a way that each fraction is unit fraction if numerator is “1” and denominator is any positive integer.
For 3/5, Egyptian fraction can be represented as 1/2 + 1/10
Back in 2000 B.C., the Egyptians had developed their fraction system for mathematical division and calculation for any rational numbers. if a cylindrical bucket has 3/5 of water full, we can understand that by dividing the bucket into 5 equal heights, the water level will reach the brim of such 3 heights. However, for the Egyptians, other numerators apart from 1 were simply inconsistent with their algorithm. Instead, they would see such volume as half a bucket full of water plus one-tenth of that, for 1/2 + 1/10 = 3/5.
i} Number should be positive
ii} Numerator should be less than denominator
Approach to solve this problem
This problem can be solved in multiple ways like using array or recursion as well. I am gonna solve it using “3 variables” and one in-built function “ceil()”.
I} Create a class (user-defined datatype) with two properties i.e. numerator and denominator.
II} Declare variables in “double” datatype for accurate result
III} Solve expression until numerator is not “zero”
IV} Before any operation, check if numerator is less than denominator or not
Source Code → Click Here
Trick to print 1–100 numbers without any function/in-built function or loop
Programming is all about how you develop logic and try out different things in it. We all know, 1–100 numbers can be easily printed out using loop or recursion but, without these, there is also a way to do so.
So, I’ve used “static variable” concept of C++ language. Static variable is property of a class and once it is created it exists till program executes. Hence, in the above code, “k” is the static variable and is incremented each time an object calls constructor internally (constructor is called internally when an object gets created).
One of the real-time applications of static variable is “no. of views on a YouTube video”, as no. of views doesn’t depend on whether user is still watching a video or not, its incremented as soon as an user visit it and exists until that YouTube channel exists or is public, etc.
Egyptian fraction is a well-known problem and has different methods to solve it as well. Also, programming is full of trick and use right approach to explore new things.