Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added LCM with test case #17

Merged
merged 8 commits into from
Jan 12, 2018
Merged

Added LCM with test case #17

merged 8 commits into from
Jan 12, 2018

Conversation

anandsit043
Copy link
Contributor

Added definition for LCM in math.hpp and test cases. Having confusion in LCM(0, a) assuming it as 0. Please review.

@codecov
Copy link

codecov bot commented Jan 5, 2018

Codecov Report

Merging #17 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #17   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          12     12           
  Lines         492    503   +11     
=====================================
+ Hits          492    503   +11
Impacted Files Coverage Δ
include/functions/math.hpp 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b218f44...c24f74d. Read the comment docs.

@anandsit043
Copy link
Contributor Author

anandsit043 commented Jan 6, 2018

Hi @faheel, Can you please point where is this check failing?


// Trivial Case handling
if(abs_num1 == 0 && abs_num2 == 0)
throw std::logic_error("LCM undefined: Both number are zero");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lcm(0, 0) exists, and is 0.

--------------------
*/

BigInt lcm(const BigInt &num1, const BigInt &num2){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lcm() function can be written more simply as just this line:

    return abs(num1 * num2) / gcd(num1, num2);

Copy link
Contributor Author

@anandsit043 anandsit043 Jan 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faheel
I changed as mentioned. Please review once. I wrote LCM function that way because it is space efficient than current.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The single line above is the most efficient as there are no conditions being checked, and has the least number of temporary variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faheel
Ok thanks.
Do I need to do something more in this. I have updated this as asked.

@faheel faheel added the functions: math Math functions label Jan 6, 2018
long long naive_lcm(long long a, long long b){
a = abs(a);
b = abs(b);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function also needs to be simplified.


BigInt lcm(const BigInt &num1, const BigInt &num2){
// Trivial Case handling
if(abs_num1 == 0 || abs_num2 == 0) return 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required.

Copy link
Contributor Author

@anandsit043 anandsit043 Jan 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if both are zero then gcd is zero and removing it gives division by zero error.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, I missed this point. But don't forget to remove the abs_ prefix or it won't compile.

// Trivial Case handling
if(abs_num1 == 0 || abs_num2 == 0) return 0;

return abs(num1 * num2)/gcd(abs_num1, abs_num2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abs_ prefix needs to be removed.

@anandsit043
Copy link
Contributor Author

anandsit043 commented Jan 9, 2018

@faheel
Done.

@anandsit043
Copy link
Contributor Author

Hi @faheel ,
Didn't heard from you. So replying again. Please look at the modified changes.

Thanks

@faheel faheel merged commit 819f74e into faheel:master Jan 12, 2018
@faheel
Copy link
Owner

faheel commented Jan 12, 2018

Sorry for the late review, I was a little busy. Thank you for contributing! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functions: math Math functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants