Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
Go
Haskell
HLSL
Hook
Hylo
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
BPF gcc (trunk)
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.8.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
Options
Source code
pub fn main() { //called multiple times with different loop counts in order to check if the compiler just removes it //if it optimized it away the measured time per loop would decrease with increased loop count helper::time_it( || { solve(); }, 100, ); helper::time_it( || { solve(); }, 1000, ); helper::time_it( || { solve(); }, 10000, ); helper::time_it( || { solve(); }, 100000, ); dbg!(solve()); } fn solve() -> usize { //the correct reduced fraction must be one of: //1/2, 1/3,..,1/9 //2/3, 2/4,..,2/9 //... //7/8,7/9 //8/9 let mut acc = fraction::Fraction::new(1, 1); for num in 1..=8 { for den in num + 1..=9 { let mut frac = fraction::Fraction::new(num, den); frac.reduce(); let num_red = frac.num(); let den_red = frac.den(); let mut i = 2; while den_red * i <= 99 { let num_i = num_red * i; let den_i = den_red * i; if num_i <= 10 || den_i <= 10 || i % 10 == 0 { i += 1; continue; } else { i += 1; } let n_1 = num_i % 10; let n_2 = num_i / 10; let d_1 = den_i % 10; let d_2 = den_i / 10; if n_1 == d_1 && n_2 == num && d_2 == den || n_2 == d_2 && n_1 == num && d_1 == den || n_1 == d_2 && n_2 == num && d_1 == den || n_2 == d_1 && n_1 == num && d_2 == den { //dbg!(num,den,num_i,den_i,num_red,den_red); acc = fraction::Fraction::new(acc.num() * num_red, acc.den() * den_red); } } } } acc.reduce(); acc.den() } mod gcd{ //This code is just copied from https://github.com/frewsxcv/rust-gcd/blob/master/src/lib.rs //since playground doesn't know gcd crate pub trait Gcd { /// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) /// using [`gcd_binary`]. /// /// [`gcd_binary`]: #method.gcd_binary /// /// # Examples /// /// ``` /// use gcd::Gcd; /// /// assert_eq!(0, 0u8.gcd(0)); /// assert_eq!(10, 10u8.gcd(0)); /// assert_eq!(10, 0u8.gcd(10)); /// assert_eq!(10, 10u8.gcd(20)); /// assert_eq!(44, 2024u32.gcd(748)); /// ``` fn gcd(self, other: Self) -> Self; /// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) /// using the [Binary GCD algorithm](https://en.wikipedia.org/wiki/Binary_GCD_algorithm). fn gcd_binary(self, other: Self) -> Self; /// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) /// using the [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm). fn gcd_euclid(self, other: Self) -> Self; } macro_rules! gcd_impl { ($($t:ty),*) => ($( impl Gcd for $t { fn gcd(self,other: Self) -> Self { self.gcd_binary(other) } fn gcd_binary(self, mut v: Self) -> Self { let mut u = self; if u == 0 { return v; } if v == 0 { return u; } let shift = (u | v).trailing_zeros(); u >>= shift; v >>= shift; u >>= u.trailing_zeros(); loop { v >>= v.trailing_zeros(); if u > v { //XOR swap algorithm v ^= u; u ^= v; v ^= u; } v -= u; // Here v >= u. if v == 0 { break; } } u << shift } fn gcd_euclid(self, other: Self) -> Self { // variable names based off Euclidean divison equation: a = b · q + r let (mut a, mut b) = if self > other { (self, other) } else { (other, self) }; while b != 0 { let r = a % b; a = b; b = r; } a } } )*) } gcd_impl! { u8, u16, u32, u64, u128, usize } } mod helper{ #[inline(always)] pub fn time_it<F: Fn() -> ()>(f: F, loops: usize) { use std::time::Instant; let now = Instant::now(); for _ in 0..loops { f(); } dbg!(now.elapsed().as_secs_f64() / loops as f64); } } mod fraction{ pub struct Fraction { num: usize, den: usize, } impl Fraction { pub fn new(num: usize, den: usize) -> Self { Self { num: num, den: den } } pub fn num(&self) -> usize { self.num } pub fn den(&self) -> usize { self.den } pub fn floor(&self) -> usize { self.num / self.den } pub fn ceil(&self) -> usize { (self.num + self.den - 1) / self.den } pub fn is_integer(&self) -> bool { (self.num % self.den) == 0 } pub fn gcd(&self) -> usize { crate::gcd::Gcd::gcd(self.num, self.den) } pub fn is_reduced(&self) -> bool { self.gcd() == 1 } pub fn reduce(&mut self) { let gcd = self.gcd(); self.num /= gcd; self.den /= gcd; } } }
rust source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
BPF gcc (trunk)
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.8.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
Options
Source code
pub fn main() { //called multiple times with different loop counts in order to check if the compiler just removes it //if it optimized it away the measured time per loop would decrease with increased loop count helper::time_it( || { solve(); }, 100, ); helper::time_it( || { solve(); }, 1000, ); helper::time_it( || { solve(); }, 10000, ); helper::time_it( || { solve(); }, 100000, ); dbg!(solve()); } fn solve() -> usize { //the correct reduced fraction must be one of: //1/2, 1/3,..,1/9 //2/3, 2/4,..,2/9 //... //7/8,7/9 //8/9 let mut acc = fraction::Fraction::new(1, 1); for num in 1..=8 { for den in num + 1..=9 { let mut frac = fraction::Fraction::new(num, den); frac.reduce(); let num_red = frac.num(); let den_red = frac.den(); let mut i = 2; while den_red * i <= 99 { let num_i = num_red * i; let den_i = den_red * i; if num_i <= 10 || den_i <= 10 || i % 10 == 0 { i += 1; continue; } else { i += 1; } let n_1 = num_i % 10; let n_2 = num_i / 10; let d_1 = den_i % 10; let d_2 = den_i / 10; if n_1 == d_2 && n_2 == num && d_1 == den || n_2 == d_1 && n_1 == num && d_2 == den { //dbg!(num,den,num_i,den_i,num_red,den_red); acc = fraction::Fraction::new(acc.num() * num_red, acc.den() * den_red); } } } } acc.reduce(); acc.den() } mod gcd{ //This code is just copied from https://github.com/frewsxcv/rust-gcd/blob/master/src/lib.rs //since playground doesn't know gcd crate pub trait Gcd { /// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) /// using [`gcd_binary`]. /// /// [`gcd_binary`]: #method.gcd_binary /// /// # Examples /// /// ``` /// use gcd::Gcd; /// /// assert_eq!(0, 0u8.gcd(0)); /// assert_eq!(10, 10u8.gcd(0)); /// assert_eq!(10, 0u8.gcd(10)); /// assert_eq!(10, 10u8.gcd(20)); /// assert_eq!(44, 2024u32.gcd(748)); /// ``` fn gcd(self, other: Self) -> Self; /// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) /// using the [Binary GCD algorithm](https://en.wikipedia.org/wiki/Binary_GCD_algorithm). fn gcd_binary(self, other: Self) -> Self; /// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) /// using the [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm). fn gcd_euclid(self, other: Self) -> Self; } macro_rules! gcd_impl { ($($t:ty),*) => ($( impl Gcd for $t { fn gcd(self,other: Self) -> Self { self.gcd_binary(other) } fn gcd_binary(self, mut v: Self) -> Self { let mut u = self; if u == 0 { return v; } if v == 0 { return u; } let shift = (u | v).trailing_zeros(); u >>= shift; v >>= shift; u >>= u.trailing_zeros(); loop { v >>= v.trailing_zeros(); if u > v { //XOR swap algorithm v ^= u; u ^= v; v ^= u; } v -= u; // Here v >= u. if v == 0 { break; } } u << shift } fn gcd_euclid(self, other: Self) -> Self { // variable names based off Euclidean divison equation: a = b · q + r let (mut a, mut b) = if self > other { (self, other) } else { (other, self) }; while b != 0 { let r = a % b; a = b; b = r; } a } } )*) } gcd_impl! { u8, u16, u32, u64, u128, usize } } mod helper{ #[inline(always)] pub fn time_it<F: Fn() -> ()>(f: F, loops: usize) { use std::time::Instant; let now = Instant::now(); for _ in 0..loops { f(); } dbg!(now.elapsed().as_secs_f64() / loops as f64); } } mod fraction{ pub struct Fraction { num: usize, den: usize, } impl Fraction { pub fn new(num: usize, den: usize) -> Self { Self { num: num, den: den } } pub fn num(&self) -> usize { self.num } pub fn den(&self) -> usize { self.den } pub fn floor(&self) -> usize { self.num / self.den } pub fn ceil(&self) -> usize { (self.num + self.den - 1) / self.den } pub fn is_integer(&self) -> bool { (self.num % self.den) == 0 } pub fn gcd(&self) -> usize { crate::gcd::Gcd::gcd(self.num, self.den) } pub fn is_reduced(&self) -> bool { self.gcd() == 1 } pub fn reduce(&mut self) { let gcd = self.gcd(); self.num /= gcd; self.den /= gcd; } } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree