Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

71%
+3 −0
Challenges Golf a FRACTRAN interpreter

Python 3, 76 71 bytes Saved 5 bytes thanks to user def f(p,n):l=[n*p//q for(p,q)in p if n%q<1];return f(p,l[0])if l else n Try it online! This code assumes that the fractions are given a...

posted 3y ago by celtschk‭  ·  edited 3y ago by celtschk‭

Answer
#2: Post edited by user avatar celtschk‭ · 2021-09-03T07:40:06Z (about 3 years ago)
Adopted improvements suggested by @user in the comments
  • # [Python 3], 76 bytes
  • <!-- language-all: lang-python -->
  • def f(p,n):l=[n*p//q for(p,q)in p if n%q==0];return n if l==[]else f(p,l[0])
  • [Try it online!][TIO-komlpd7s]
  • This code assumes that the fractions are given as completely cancelled pairs of integers.
  • [Python 3]: https://docs.python.org/3/
  • [TIO-komlpd7s]: https://tio.run/##ZVJdb4MgFH33V9yQLJGFpAIFPxZe97x3Y4hZcTOxaJU@LEt/ewdqW5uZIHK8nHMuh@HHffeWX68H00ATD8TiolOlfR12uxM0/eihE24tDNA2YF9OSiXV22jcebRgA9YpVVamm8y8vSuTCs9kzkwuDq/PejK4iMA/w9jaB0jA2INCBSC8@Ys@6mkyBxS4m3ttiYax/xrrI6oIPMDWDmeHKgxKbdD@7GYYZlvovW47T4ijKPg61q2NVz@3LRQU/MJdooAyTjMiBCYQC8LDRMm8opSwBU2xd7IaKIAmuaRcpJx7cNUvgLMs2/uR0Vymku1TJrPcF6UJh8uTA/bfAfdKTxIsl1tyKSR9JuH/SfbC@@erc7p2QmmYuW9h0xH1fW7lUrYVo0IycYlmOX8rwIG/E@X9/B6ZsMcnr5ZTvlmMnY/Ap6q1rY9G6xAa0joEojVaapd0ousf "Python 3 – Try It Online"
  • # [Python 3], <s>76</s> 71 bytes
  • Saved 5 bytes thanks to [user](https://codegolf.codidact.com/comments/thread/3115#comment-10312)
  • <!-- language-all: lang-python -->
  • def f(p,n):l=[n*p//q for(p,q)in p if n%q<1];return f(p,l[0])if l else n
  • [Try it online!][TIO-kt41kxaz]
  • This code assumes that the fractions are given as completely cancelled pairs of integers.
  • [Python 3]: https://docs.python.org/3/
  • [TIO-kt41kxaz]: https://tio.run/##ZVJda4MwFH33V1wCAzMCbZImfmy@7nnvIkHWuAk2Wk0fxuhv764fbS0VYszJ9Zxzc9L9@p/Wyctlbyuowo45mjZZ7l67zeYIVdsjdKS1gw7qCtzL8Z0Xb731p95N5U2@LSjuNGCbwYKbeLwdfDi@vsrB0jQAfLq@dneQgXX7jKRA6GqXfJbDYPdklKputTnp@va7Lw@kYHAHa9edPCkoZNkKbU9@gmc75KOsGySkQTD6OpS1Cxc/1184ZPAHN4kU8jCKmVKUQaiYHCfOphXnTMxoRNHJYiAFvk00lyqSEsFFPwUp4niHI@aJjrTYRULHCRZFWwnnBwfi2YFEpQcJkeg1uVaaP5LIZ5KdQv9ycc6XTjgfZ4ktrDri2OdaLhJrMa60UOdgksMLAR7wOuS387tnIu6fsphP@Wox9BgBpmqMKw/WmDE0YswYiDFkrp3TCS7/ "Python 3 – Try It Online"
#1: Initial revision by user avatar celtschk‭ · 2021-05-13T08:01:04Z (over 3 years ago)
# [Python 3], 76 bytes

<!-- language-all: lang-python -->

    def f(p,n):l=[n*p//q for(p,q)in p if n%q==0];return n if l==[]else f(p,l[0])

[Try it online!][TIO-komlpd7s]

This code assumes that the fractions are given as completely cancelled pairs of integers.

[Python 3]: https://docs.python.org/3/
[TIO-komlpd7s]: https://tio.run/##ZVJdb4MgFH33V9yQLJGFpAIFPxZe97x3Y4hZcTOxaJU@LEt/ewdqW5uZIHK8nHMuh@HHffeWX68H00ATD8TiolOlfR12uxM0/eihE24tDNA2YF9OSiXV22jcebRgA9YpVVamm8y8vSuTCs9kzkwuDq/PejK4iMA/w9jaB0jA2INCBSC8@Ys@6mkyBxS4m3ttiYax/xrrI6oIPMDWDmeHKgxKbdD@7GYYZlvovW47T4ijKPg61q2NVz@3LRQU/MJdooAyTjMiBCYQC8LDRMm8opSwBU2xd7IaKIAmuaRcpJx7cNUvgLMs2/uR0Vymku1TJrPcF6UJh8uTA/bfAfdKTxIsl1tyKSR9JuH/SfbC@@erc7p2QmmYuW9h0xH1fW7lUrYVo0IycYlmOX8rwIG/E@X9/B6ZsMcnr5ZTvlmMnY/Ap6q1rY9G6xAa0joEojVaapd0ousf "Python 3 – Try It Online"