r/dailyprogrammer • u/fvandepitte 0 0 • Jul 25 '16
[2016-07-25] Challenge #277 [Easy] Simplifying fractions
Description
A fraction exists of a numerator (top part) and a denominator (bottom part) as you probably all know.
Simplifying (or reducing) fractions means to make the fraction as simple as possible. Meaning that the denominator is a close to 1 as possible. This can be done by dividing the numerator and denominator by their greatest common divisor.
Formal Inputs & Outputs
Input description
You will be given a list with 2 numbers seperator by a space. The first is the numerator, the second the denominator
4 8
1536 78360
51478 5536
46410 119340
7673 4729
4096 1024Output description
The most simplified numbers
1 2
64 3265
25739 2768
7 18
7673 4729
4 1Notes/Hints
Most languages have by default this kind of functionality, but if you want to challenge yourself, you should go back to the basic theory and implement it yourself.
Bonus
Instead of using numbers, we could also use letters.
For instance
ab a
__ = _
cb c And if you know that x = cb, then you would have this:
ab a
__ = _
x c and offcourse:
a 1
__ = _
a 1
aa a
__ = _
a 1The input will be first a number saying how many equations there are. And after the equations, you have the fractions.
The equations are a letter and a value seperated by a space. An equation can have another equation in it.
3
x cb
y ab
z xa
ab cb
ab x
x y
z y
z xayoutput:
a c
a c
c a
c 1
1 abFinally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
7
u/Mister_Spacely Aug 03 '16 edited Aug 03 '16
New to this sub, first submission. Really went into detail on this and used file arguments for my main. Here is my C code:
2
u/kilo_59 Sep 06 '16 edited Sep 06 '16
I think I'm late to the party. First submission, feedback welcome.
Python 3.5
2
u/4-Vektor 1 0 Jan 10 '17 edited Jan 10 '17
DUP
The esoteric stack-based programming language DUP is a dialect of the famous FALSE language. Both DUP and FALSE share a lot of similarities with Forth.
First, the functional solution:
Explanation, using example
4096 1024:While the functional approach uses a bit more complicated mechanisms, the operator definition method has simpler instructions and works basically the same. DUP allows only lowercase ASCII characters for function names, while the definition of new or overriding of old operators allows the use of the whole Unicode range of codepoints.
The detailed differences between both approaches are irrelevant for this example.
The operator
⇒assigns a lambda/function to an operator. In this example: capital gammaΓis the GCD operator, capital phiΦis the fraction operator.As can be seen, operator calls are syntactically simpler than function calls. Apart from that, both examples work identically:
25
u/Flynn58 Jul 25 '16 edited Aug 03 '16
Python 3
Lazy? Yes. Just as a programmer should be.
8
u/a_Happy_Tiny_Bunny Jul 25 '16
Haskell
91 characters of casual golfing:
8
u/pulpdrew Jul 25 '16
Java solution, no bonus
6
u/namekuseijin Jul 25 '16
in scheme
(/ 4 8) => 1/2
scheme got a full numeric tower with exact and inexact arithmetic.
maybe I'll try the bonus
5
u/Godspiral 3 3 Jul 25 '16 edited Jul 25 '16
in J, the builtin is actually longer than using gcd.
64 3265
1 2
bonus without substitution step,
1 ab
d ab
better bonus version,
5
u/syholloway Jul 26 '16
PHP
Put any values in via stdin:
4
u/[deleted] Jul 25 '16
Python 3, the obvious way. Too lazy to implement a bonus (for now).
4
u/Scroph 0 0 Jul 25 '16
Bonusless C++. I hope I got the logic right :
I'll add the bonus if I figure it out.
4
u/roydl7 Jul 25 '16
C89
1
u/howyoudodis Jul 28 '16
Could I get some help with this? I just started Java programming and I have this much so far. I am not sure how to simplify the fraction. I have gotten to the point where I am dividing it, but I think I need a greatest common divisor like someone who did this in Java. Currently stuck and don't know how to proceed with the last part of my code.
1
u/[deleted] Aug 08 '16
Rust, without bonus. Because wtf letters? :p
https://github.com/archer884/reduce
This makes use of another crate I wrote for this project found here:
https://github.com/archer884/iter-ord
1
u/anhkim13 Oct 19 '16
Java, this is my first try and kinda late for the group but I am a newbie so feel free to give me any feedback to help optimize my code . thank
1
u/4-Vektor 1 0 Jan 12 '17 edited Jan 12 '17
beeswax
beeswax is a 2-dimensional stack-based self-modifying fungeoid (as in “similar to Befunge”) esolang on a hexagonal grid. An arbitrary amount of instruction pointers (called bees) get created at program start, moving across the source code space (the honeycomb). Each bee carries a local stack (lstack, ls for convenience here) of length 3 to carry out all kinds of arithmetic, bit manipulation etc. All bees can access a global stack of arbitrary length (gstack/gs). The gstack is only able to store/fetch values to and from bees, and can move around its own values, but is not able to manipulate values in any other way, so any bit manipulation etc. has to be executed by the bees.
For more detailed information please refer to the beeswax esolangs page or the beeswax interpreter page on GitHub.
Explanation
Here’s an example execution of this program, reducing the fraction 64/12. For this example, I have named the bees that get created in this program α and β, α doing the main work:
As you can see, the block that’s entered below the
#is actually a loop (moving down from the cloning instruction
W) that calculates the GCD.β(moving fromWto the upper left towards thef) only pushes the temporary value on the global stack. Thegin the loop fetches it from the gstack, the?pops the temporary value to make place for the next one.The actual execution looks like this in the console (program stored as
fractions.bswx):The little
ibefore the input values only indicates that the program expects an integer value as input and is part of the interpreter behavior.3
u/LordKJ Jul 25 '16 edited Jul 25 '16
Python 3, rather new to the language, might look at the bonus a bit later
output:
// ++bonus
output:
im pretty sure it can be done in like 5 lines, but well if you dont know what functions are you looking for, you wont find them. :/
3
u/ItsOppositeDayHere Jul 28 '16
C# (no bonus)
2
u/thorwing Jul 25 '16 edited Jul 25 '16
java 8 (Now with bonus)
bonus
2
u/Epthelyn Jul 25 '16 edited Jul 25 '16
Java with bonus
Supports numeric inputs, non-numeric 'standard' inputs (e.g. a/ab = 1/b) and the substitution-based simplification for the bonus.
Doesn't support a combination of the two styles (e.g. 2ab/4bc = a/2c) however.
GCD using a recursive implementation of Euclidean algorithm for numeric, and then a mess for the rest - I'm sure there's some neater way to do the bonus, but I'll take "it works" for now!
It's a little long, so the code is here: http://pastebin.com/HcHe0thx
Output:
EDIT: Fixed output
2
u/Paul_Dirac_ Jul 26 '16
Ye olde Fortran (95) But only part one.
2
u/paulggardner Jul 27 '16
EMACS Elisp. No bonus
2
u/EraZ3712 Jul 27 '16
Hmm, the bonus solution looks pretty tricky, but for now, here's a C++ solution for the normal challenge. It calculates the greatest common divisor using the recursive Euclidean Algorithm. Since the problem spec did not mention how to handle negative numbers, I've simply left the signs as-is:
2
u/[deleted] Jul 30 '16
Ruby
2
u/djtelly Aug 23 '16
Java
2
u/APIUM- Aug 24 '16
Python 3
Now it might not be beaut, but just picked Python back up a couple of days ago after a long Hiatus....
Critique appreciated.
2
u/snakeyblakey Sep 02 '16
brand new (like, started reading on Sunday) to any language, starting in javascript. any feedback is welcome
2
u/JrodManU Sep 05 '16
Java (no bonus) First challenge, sorry if it's messy!
2
u/Kennefofearf Sep 09 '16 edited Sep 09 '16
Swift
2
u/Splash_Donkey Sep 21 '16
Python
2
u/addcn Sep 24 '16
Python 3
2
u/d_thinker Sep 24 '16 edited Sep 25 '16
Python 3
I couldn't think of proper mathematical way to do this so I looped like crazy. Its really ugly but it works.
2
u/955559 Nov 17 '16
Python 3fractions = [ [4, 8], [1536, 78360], [51478, 5536], [46410, 119340], [7673, 4729], [4096, 1024], ]
1
u/EvgeniyZh 1 0 Jul 25 '16
1
u/saihtame Jul 25 '16
C#
1
u/El_Dumfuco Jul 25 '16
Matlab
1
u/Craftyzebra1992 Jul 25 '16
Python 3
1
u/abyssalheaven 0 1 Jul 25 '16
Python 3 no bonuses, feels a little convoluted but I think it's kind of fun =)
tried to do it a little differently
+/u/CompileBot Python 3
1
u/nwsm Jul 25 '16 edited Jul 25 '16
+/u/CompileBot Java
1
u/Jcisneros1 Jul 25 '16 edited Jul 25 '16
Powershell, still new to programming so please feel free to criticize.
1
u/HDean_ Jul 25 '16
Python 3
1
u/frankyfrankfrank Jul 25 '16
PHP Will refactor to collections and attempt bonus - (First Submission sorry if bad formatting)
1
u/tohearstories Jul 25 '16
i.m a junior CS student, so my code is probably way more basic than everyone else.s, but here is my solution in Java. any feedback is appreciated.
1
u/Toasted_FlapJacks Jul 25 '16 edited Jul 27 '16
JAVA No bonus and assuming neither value is zero.
EDIT: Figured out that my previous solution would not work with large prime numbers.
1
u/KatsTakeState Jul 25 '16
Python 3
def simplify(numerator,denominator): for i in reversed(range(0,denominator+1)): if denominator % i==0 and numerator % i == 0: return (str(int(numerator/i))+'/'+str(int(denominator/i))) print('SIMPLIFIED: '+simplify(num,den))
Might hit the bonus once I understand it
1
u/-Polyphony- Jul 26 '16
Common Lisp example for very simple fraction simplification. :)
1
u/augus7 Jul 26 '16 edited Jul 26 '16
Here's mine:
Python:
BONUS:(kinda rushed)
OUTPUT:
1
u/draegtun Jul 26 '16 edited Jul 26 '16
Rebol
Example usage (in Rebol console):
NB. Tested in Rebol 3
1
u/4kpics Jul 26 '16
C89, no bonus. Compile with
cc frac.c1
u/pi_half157 Jul 26 '16
Python 3.5 with bonus. I understand that there is a two line method that starts with a g that I could use, but I decided against it. If anyone would like to justify the complexity of this algorithm, please let me know so I don't feel so bad.
Bonus:
1
u/zimmzz Jul 27 '16
C++ no bonus
https://gist.github.com/z1mmz/1727c54869aade46cd9ff3f4965c043b
1
u/zeroskillz Jul 27 '16
Javascript (es6) no bonus
1
u/EraZ3712 Jul 27 '16
Wrote a bonus solution in C++11! I had to make several assumptions about the problem though:
'a'to 'z'.Given these assumptions, here is my solution!
1
u/whatswrongwithgoats Jul 27 '16 edited Jul 28 '16
Python 3 - no bonus.
Output:
Edit: Cleaned up the output to int. Thanks to /u/LordKJ for finding the problem.
1
u/Trying2LearnJava Jul 27 '16
Java no bonus new to this so any tips would be helpful
1
u/[deleted] Jul 27 '16 edited Jul 27 '16
Fortran 90:
1
u/purg3be Jul 27 '16 edited Jul 27 '16
+/u/CompileBot
Java
1
u/_bush Jul 27 '16
In poorly coded C: //simplify fractions
Does not work for big numbers, but it's something :D
1
u/ChipHappens Jul 28 '16
C# (No bonuses)
1
u/niandra3 Jul 28 '16
I thought this was somewhat elegant:
+/u/CompileBot Python3
Bonus to come...
1
u/ruby-solve Jul 29 '16
Ruby, done functionally
1
u/taliriktug Jul 29 '16
Rust
1
u/tajjet Jul 29 '16
Python 3
1
u/slowboard Jul 29 '16 edited Jul 29 '16
Python 3 +/u/CompileBot python 3
Using Euclids algorithm for the greatest common divisor and some string splitting to get the inputs
1
u/rubblebath Jul 30 '16
Python 3 Probably came up with the least efficient way to do this, but fun to try!
+/u/CompileBot Python3
1
u/sdlambert Jul 30 '16
Solution in Javascript (node/es6)
1
u/Skoogy_dan Jul 30 '16
Java, i'm a beginner so tips and tricks are welcome. No bonus.
1
u/tritanjent Jul 31 '16
C++
1
u/Banana-Bro Jul 31 '16
Python 3 no bonus
1
u/gbladeCL Jul 31 '16
Perl6 with bonus
Solution shows using built-in Rat (rational) type, built-in gcd function, and a custom gcd function. Use of multi subs allows cleanup of if statements.
1
u/Dinokak Aug 01 '16
C#
Output
1
u/OrangeFoil Aug 03 '16 edited Aug 03 '16
Subroutine in MIPS32 Assembler
1
u/Kayodic Aug 04 '16
Hi, this is my first submit, I would like to get some feedback! I made it in Haskell.
1
u/devster31 Aug 04 '16
Go / Golang
I'm learning the language so the solution is long and can probably be improved, feel free to comment.
1
u/yangxiaoyong Aug 05 '16
bouns part
1
u/vishal_jaiswal Aug 05 '16
Without bonus in R
1
u/plafiff Aug 05 '16
Java w/no bonus
1
u/sallystudios Aug 07 '16
C++ I was able to solve this in about 20 minutes, which has been my fastest so far!
1
u/Zeby95 Aug 07 '16
C without bonus.
1
u/tijo799 Aug 08 '16
Go
A few days late — just getting started with Go.
Output:
1
u/binari101 Aug 08 '16
1
u/tadm123 Aug 09 '16 edited Aug 09 '16
C89
1
u/Guipa Aug 09 '16 edited Aug 10 '16
My not so beautiful submission in Python 3:
1
u/desterox Aug 09 '16 edited Aug 09 '16
C++
Edit: Kinda new, any opinions?
1
u/animejunkied Aug 09 '16
First time submission. Here is my java code with bonus. Feel free to critique :)
1
u/StopDropHammertime Aug 09 '16 edited Aug 09 '16
F#. I didn't bother with the letter substitution part
1
u/Thedorekazinski Aug 10 '16
C. Normally wouldn't post a solution to a two week old problem, but I had already made this little utility a while back to do the grunt work for some math homework.
1
u/Hezerac Aug 10 '16 edited Aug 15 '16
C++
1
u/[deleted] Aug 11 '16
My first submission. Written in C with no bonus.
1
u/Tony_Sax Aug 11 '16
C++
First post. Feedback appreciated along with tips on using gist.
1
u/ivankahl Aug 12 '16 edited Aug 12 '16
Bonusless answer in C#:
1
u/Blackshell 2 0 Aug 12 '16
So I'm learning reactive programming (thanks, Angular 2), and I decided to try my hand at doing it in Python using the RxPY library:
Or, condensed into one line:
I now have a headache.
1
u/[deleted] Aug 13 '16 edited Aug 17 '16
Java
Hey all, I am an incoming second year comp sci major with no prior background in computer science. Here is my solution for the easy challenge. All feedback is welcome and would be very helpful!
edit:just realized the redundant code I have if anyone ever sees this
1
u/bcgroom Aug 13 '16
Java
I'm a little rusty, about to start up my second university course in 2 weeks again. Not a super elegant solution but it's what I came up with off the top of my head
1
u/mks1992 Aug 15 '16 edited Aug 15 '16
Lua Simple Lua script with no bonus solution.
1
u/shiniko Aug 17 '16
No bonus C++. I have not coded in a while, so I need to get back into it!
1
u/Zalkota Aug 19 '16
I found this on stockoverflow and I am confused as to what "long" is doing. Thanks
1
u/konqoro Aug 19 '16
Tried this in Nim for the first time, I don't quite like it. All suggestion are welcomed.
1
u/totallygeek Aug 24 '16
Perhaps late, but I did this up in Bash.
Output:
1
u/Trezker Aug 28 '16
+/u/CompileBot D
1
u/More_Coffee_Than_Man Aug 29 '16
Python 3. Going through a course on it right now or I probably would've just written it in C. Could probably be tightened up. This seems like it would be a textbook use case for recursion, but this was my scratch code and it worked, so, whatever.
1
u/unfallenrain20 Aug 29 '16 edited Sep 09 '16
+/u/CompileBot Python 3
1
u/Nhowka Sep 13 '16
F# No bonus, but using active patterns for ignoring invalid input and some modifications for working with negative numbers correctly. If both are negative, get the positive form.
1
u/yeah_i_got_skills Sep 24 '16
Python 3
1
u/[deleted] Sep 26 '16
recursive in golang without bonus
1
u/ramkiller1 Sep 30 '16 edited Sep 30 '16
First time c#. Still trying to figure out the best way to extract the numbers out of the text easily.
1
u/Soro300 Sep 30 '16
Python
Just "finished" learning Python and looked for a simple task xD
1
u/[deleted] Oct 03 '16
Java - I tried doing reddit block code formatting and it killed my code indentation, which sucks. I'm fairly new at this but here was my stab at it.
1
u/phenomaks Oct 03 '16
Rough C++ Solution for Part 1
1
u/sainath_komuravelly Oct 09 '16 edited Oct 09 '16
My First submission. Looks bit lengthy but happy that it works.
1
u/xilni Oct 13 '16
Swift 3
1
u/[deleted] Oct 18 '16
C# (No Bonus)
1
u/saiij Oct 21 '16
JAVA
Hey dudes I am pretty new to programming and Java. I just wanted to show you my solution and maybe someone can tell me how this code can be optimized.
cheers!
1
u/rtk94 Oct 26 '16
Well, this thread is a few days old now, but I've got a solution in C++ with a bit of my own spin on things.
My solution asks the user for a numerator and denominator. Once the program has these, it finds the GCD using a function I defined.
Finally, it simplifies the fraction by either finding a whole number equivalent, a whole number and a simplified fraction, or just a simplified fraction.
I'd appreciate some feedback!
1
u/karrash76 Oct 26 '16 edited Oct 26 '16
JAVA beginner solution with Euclidean Algorithm. Comments will be greatly appreciated ;)
1
u/SpaniardFapstronaut Nov 07 '16
JAVA
class Prueba { public void simplifica (int numer, int denom) { int maxf=1;
}
class Solution { public static void main(String []args) { Prueba p1=new Prueba(); p1.simplifica(32, 8); } }
1
u/codetrasher Nov 23 '16 edited Nov 23 '16
Here's my solution using Go (without bonus)
1
u/Dswimanator Nov 23 '16
Java, First submission.
import java.util.Scanner;
public class Fractions {
}
1
u/Xmer Nov 29 '16
C#, first post. Feedback welcomed: Gist
1
u/AquaQuartz Dec 01 '16 edited Dec 01 '16
Python 3. Nothing elegant about my solution, but I'm a beginner, so I'm okay with it.
Edit: Fixed a typo
1
u/mochancrimthann Dec 04 '16
Ruby, no bonus
Or
1
u/YuukiRus Dec 05 '16
C#
This is just two functions that simplify and print the result respectively. They do not accept a string input and I know that invalidates it, but I am on a programming blitz and minor details I do every single challenge I am skipping.
1
u/mochancrimthann Dec 05 '16
Clojure, no bonus. I'm new to Clojure, I would greatly appreciate pointers and feedback!
1
u/TroopOfChimps Dec 07 '16
JavaScript:
1
u/[deleted] Dec 09 '16 edited Dec 09 '16
C#
Finally finish. Sadly, I had a really dump little mistake, why it took me so long...However, I'm happy, I'm able to programm this, because I started programming only 6 weeks ago. I'm open for every tip!
namespace CA_031216_Symplifying { class Simplifying { static void Main(string[] args) { // User can choose own fraction int num = Input("Enter your fraction \n numerator"); int den = Input("denominator");
}
1
u/andrijar20 Dec 12 '16 edited Dec 12 '16
https://gist.github.com/andrijar20/a1f20654ae5bf35e54222cd1749d217c This is how i solved it . Please give me your opinions because I'm very new to programming.
1
u/Towerz Dec 22 '16 edited Dec 22 '16
Written in C. I'm very new to programming as a whole, and this is probably very inefficient but screw it, I'm on break. I didn't realize how late this submission was until I posted so oops, but I did it so I'm posting anyway.
1
u/[deleted] Dec 23 '16
Java with 2D array ...
package dailyprogrammer; import java.util.Scanner;
public class Easy277 {
}
1
u/AntillaOnAsiaa Jan 03 '17
Python3 (no bonus), I am sure that it would be more efficient. But I think this makes more sense. At least to my kind of Python noobs. Of course, comments are welcome.
1
u/[deleted] Jan 12 '17
Common Lisp
1
u/kyle2143 Jan 12 '17
Bit late, but I saw this and thought I'd have a go.
Java, tried to make it as simple as possible from scratch; did a bit to make it efficient where completely obvious but didn't really think too much into what could be done to do it really fast.
1
u/TheMadMaritimer Jan 13 '17 edited Jan 13 '17
First time posting, just saw this across the top and thought it was the most recent, oh well, hopefully someone sees it!
It's my first go at writing anything in C# (coming from Java), so be gentle!
C#, No Bonus
1
u/CaptainMoeSoccer Jan 16 '17
I am too lazy to turn the float into an integer. python3
1
u/WimbleCF Jan 20 '17
Initially I had problems with (51478, 5536) but realized what I did wrong and fixed it. I was starting with 1 when I should have been starting at 0).
Spoiler
0
u/[deleted] Jul 25 '16
[deleted] — view deleted comment
0
u/[deleted] Jul 25 '16
[deleted] — view deleted comment