c++ - Smallest number that is evenly divisible by all of the numbers from 1 to 20? -
I had this problem [], but very poor programming method, looking at code in C ++,
# include & lt; Iostream & gt; using namespace std; // lowest divisble number up to 20 int () {int number = 20, flag = 0; While (flag == 0) {if ((2%) == 0 & amp; amp; & amp; amp; & amp; amp; & amp; amp; amp; amp; amp; amp; amp ; And amp; (number% 4) == 0 & amp; amp; amp; amp; amp;) == 0 & amp; (Number% 6) == 0 & amp; Amp; Amp; (Amp; 7%) == 0 & amp; Amp; Amp; (Number% 8) == 0 & amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; = 0 & amp; Amp; (Num% 10) == 0 & amp; Amp; Amp; Amp; Amp; (Amp; number% 11) == 0 & amp; (Number% 12) == 0 & amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; & Amp;; & Amp; Amp; Amp; Amp; (Number% 14) == 0 & amp; Amp; Amp; Amp; Amp; Amp; (Amp; %% 15) == 0 & amp; (Number% 16) == 0 & amp; (Number% 17) == 0 & amp; & Amp; (Number% 18) == 0 & amp; Amp; Amp; (Number% 19) == 0 & amp; Amp; Amp; Amp; Amp; Amp; & Amp; (Number% 20) == 0) {flag = 1; The court's & lt; & Lt; "The lowest divisible number is up to 20" & lt; & Lt; The number & lt; & Lt; Endl; } Num ++; }}
I was solving it in C ++ and was trapped in a loop, how would this step solve? ...
- Think
if ((num% 2) == 0 & amp; (number% 3) == 0 ; Amp; amp; (num% 4) == 0 & amp; amp; & amp; amp; amp; & amp; amp; amp; amp; amp; amp; amp; amp ; & Amp; (Number% 6) == 0 & amp; 7) == 0 & amp; (Num% 8) == 0 & amp; Amp; Amp; Amp; (Amp; 9% 9) == 0 & amp; Amp; Amp; Amp; Amp; Amp; Amp; & Amp; (Number% 10) == 0 & amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; & Amp; == 0 & amp; amp; (Number% 12) == 0 & amp; Amp; Amp; Amp; (Amp; number% 13) == 0 & amp; (Num% 14) == 0 & amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp & amp ;; 0 & amp; Amp; Amp; (Number% 16) == 0 & amp; Amp; Amp; Amp; (Number% 17) == 0 & amp; (Number% 18) == 0 & amp; (Number% 19) == 0 & amp; ; & Amp; (Number% 20) == 0) `
How to code it properly?
The answer to this problem is:
Abhilash @ Abhilash: ~ $ ./a.out The lowest division Ible number 20 is 232792560
A quick way to answer this problem is, number theory Other answers are signs of how to do it. This answer is a better way to write the status if
in your code.
If you want to change only the long term situation, you can better express it to the loop:
if ((num% 2) == 0 & amp; (num% 3) == 0 & amp; (num% 4) == 0 & amp; amp;; (number% 5) == 0 & amp; amp; amp; ; 6%) == 0 & amp; amp; amp; (num% 7) == 0 & amp; amp; & amp; amp; amp; amp; amp; amp; amp; amp; amp ; Amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;% number 9) == 0 & amp; amp; ; (Number% 10) == 0 & amp; Amp; Amp; (Amp; number% 11) == 0 & amp; Amp; Amp; (Number% 12) == 0 & amp; Amp; 13) == 0 & amp; (Num% 14) == 0 & amp; Amp; Amp; Amp; (Amp; number% 15) == 0 & amp; Amp; Amp; Amp; Amp; (Num% 16) == 0 & amp; Amp; Amp; Amp; Amp; & Amp; (Number% 17) == 0 & amp; Amp; (Number% 18) == 0 & amp; Amp; Amp; Amp; (Number% 19) == 0 & amp; (Number% 20) == 0) {...}
becomes:
{int divisor; Break (num% divisor! = 0) for (divisor = 2; divisor & lt; = 20; divisor ++); If (Separator! = 21) {...}}
The style is not very good, but I think this is what you wanted.
Comments
Post a Comment