Giter Site home page Giter Site logo

rustp's Introduction

Hi there, This is Naman Garg Waving Hand

๐Ÿ‡ฎ๐Ÿ‡ณ ย  I am from Kaithal, Haryana, India
๐Ÿ‘จโ€๐ŸŽ“ ย  I am studying at National Institute of Technology, Kurukshetra
๐Ÿ’ป ย  I am pursuing Bachelor of Technology in Computer Engineering
๐Ÿ˜Ž ย  I am Enjoying what I do.

My Projects

Programming Languages

My Profiles

Here are some fancy GitHub stats


rustp's People

Contributors

namanlp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

alchem-1st

rustp's Issues

Use `std::mem::swap` instead of temp variables

Hi! I just came across your website from the Rust Users forum. It's pretty cool!

I noticed a spot where the code provided for Euclid's GCD algorithm could be improved: you use temporary variables to swap a and b, but we can just use std::mem::swap to more idiomatically (and maybe slightly more quickly) swap the two.

Here's what the simplified code would be:

fn gcd(mut a: usize, mut b: usize) -> usize {
    // If equal, return any of them
    if a == b {
        return a;
    }

    // Swap a with b, if b is greater than a
    if b > a {
        std::mem::swap(&mut a, &mut b);
    }

    while b > 0 {
        a %= b;
        std::mem::swap(&mut a, &mut b);
    }

    // Now, a % b = 0, hence return it
    return a;
}

(I would also substitute the first two checks with match a.cmp(&b) to save time, but this is mostly a matter of taste)

While we're at it, it might be worthwhile that most modern implementations of GCD these days take advantage of O(1) bit-scan operations - in the case of Rust, trailing_zeros() - to quickly implement the binary GCD algorithm. Check out how num does it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.