SEC-S20W3 Writing the first code

in #sec20w3sergeyk3 months ago (edited)

Hello everyone! I hope you will be good. Today I am here to participate in the contest of @sergeyk about the Writing the first code. It is really an interesting and knowledgeable contest. There is a lot to explore. If you want to join then:



Join Here: SEC-S20W3 Writing the first code



digital tech book logo design template.png

Designed with Canva

1. Online compiler (IDE)

There are a number of online compilers which supports many languages languages from basic to advance. I also use different online compilers for my daily usage. I mostly use Programiz and Onecompiler for the execution of the programs online with ease of use. So here I am sharing Onecompiler to complete the first task of the course because in it everything is separately available.

image.png

In the above picture I have indicated all the required things. I have chosen C++ language. The first left side window is for writing the code. At the right side at the lower side there is an output window. And at the right top side there is a RUN button to run and compile the program. And just right below the RUN button there is a data entry window. Data entry window is used for the user inputs. If we need to get user input then we can enter the inputs in the data entry window.

In some compilers data entry window and the output window are the same. The same window is used to enter the user inputs and for the output results. And in my opinion these are the best compilers which have combined input and output window because when a program runs line by line we can easily input the values where required.

Lesson should be completed in three versions

find your way to act, from 1 to 100 effective or not in this task is not important.

This is the question from the last lesson but here we have to solve this question in 3 versions. So I have improved my finding and solution for this question to traverse from 1 to 100 only using +1 and x2. In the last lesson I completed traversing from 1 to 100 in 14 steps. But here I have found another way to reach from 1 to 100 just by using +1 and x2. So here are the steps to reach from 1 to 100:

1+1+1 = 3 * 2 = 6 * 2 = 12 * 2 = 24 * 2 = 48 * 2 = 96 + 1 + 1 + 1 + 1 = 100

Here I have reached from 1 to 100 with the 12 steps which are shorter than the previous attempt in the last lesson. I have used multiple times +1 and x2 according to my requirements to reach from 1 to 100.

If I tell you step by step then here are the steps:

Here’s the breakdown of your operations step by step:

  1. Start with 1.
  2. Add 1:
    ( 1 + 1 = 2 )
  3. Add 1:
    ( 2 + 1 = 3 )
  4. Multiply by 2:
    ( 3 x 2 = 6 )
  5. Multiply by 2:
    ( 6 x 2 = 12 )
  6. Multiply by 2:
    ( 12 x 2 = 24 )
  7. Multiply by 2:
    ( 24 x 2 = 48 )
  8. Multiply by 2:
    ( 48 x 2 = 96 )
  9. Add 1:
    ( 96 + 1 = 97 )
  10. Add 1:
    ( 97 + 1 = 98 )
  11. Add 1:
    ( 98 + 1 = 99 )
  12. Add 1:
    ( 99 + 1 = 100 )

So, the final result is 100.

find the best, shortest way. No one found it in the last lesson. Search!

You can see that I have already reduced 2 steps while reaching from 1 to 100 from 14 steps to 12 steps. But now I Have to find any other way which is the most shortest way to reach 100 starting from 1 by using +1 and x2 and I cannot use any other operations.

So I started trying again and again on the calculator of my mobile to reach hundred by using different strategies. It took me some minutes to figure out another shortest way of reaching 100 starting from 1 by using +1 and x2. And as far as I think and tried again and again I did not find any other short way than this.

1+1+1 = 3 * 2 = 6 * 2 = 12 * 2 = 24 +1 = 25 * 2 = 50 * 2 = 100

I have reached 100 in just 9 steps while starting from 1 and using only +1 and x2.

Here is the breakdown of the operations step by step:

  1. Start with 1.
  2. Add 1:
    ( 1 + 1 = 2 )
  3. Add 1:
    ( 2 + 1 = 3 )
  4. Multiply by 2:
    ( 3 x 2 = 6 )
  5. Multiply by 2:
    ( 6 x 2 = 12 )
  6. Multiply by 2:
    ( 12 x 2 = 24 )
  7. Add 1:
    ( 24 + 1 = 25 )
  8. Multiply by 2:
    ( 25 x 2 = 50 )
  9. Multiply by 2:
    ( 50 x 2 = 100 )

Here I have reduced more 3 steps to reach 100 from 1 while using +1 and x2. And if I compare my these steps to my first calculation in the previous lesson then I have reduced 5 steps and it is a wonderful achievement for me. But I am not sure if my teacher @sergeyk has another way to reach 100 with more shortest way.

I have carefully used +1 and x2 to reduce the number of the steps. So in this way we can use different strategies to solve our problems in the shortest way. The programmer who solves the problems in the shortest way and in minimum number of lines of code is considered as the best programmer and that programmer is paid well.

if in addition to +1and x2it is possible to create an additional variable int k;, how many steps will it save, you cannot k=n put a certain number in the variable, for example 7 or 23. You can only fix the value of the variable at a certain step n

It was another critical task and it needed to think a lot to use the new variable in the calculation to reduce the number of the steps. So again I hold my mobile and started doing the calculation on the mobile calculator to find a new way to reduce the steps with the help of the variable int k;. Actually at start I tried many ways but the steps were not decreasing they were coming 9 or more. So after struggling for some time and using the creativity of my mind I suddenly got an idea and as soon as I tried that idea I became very happy that I have decreased the steps more.

So here is my idea:

First of all I used +1 to count from 1 to 5 and then I stored 5 in variable k such that k = 5 and then again I multiplied 5 with variable k to get 25 and then I used x2 with 25 to get 50 and again x2 with 50 to get 100.

So here are the steps:

  • 1 + 1 + 1 + 1 + 1 = 5
  • Here I am setting k = 5.
  • 5 * k = 25
  • 25 * 2 = 50
  • 50 * 2 = 100

So I have reduced 1 more step and I tried many strategies but each strategy was giving me no less than 8 steps or higher. So I have chosen the above steps.

image.png

  • I have total of 538 followers and taking first two the number becomes 53.

  • Then according to the requirement I have added 21 to it which is the number of my age haha. So 53 + 21 = 74

  • So now its time to break down 74.

I have to reach to 74 starting from 1 by using +1 and x2. If I see the factorization of 74 it is 1 x 2 x37 = 74. In order to reach 74 I have added these simple steps using only +1 and x2 and these steps are given below:

  • 1 x 2 x 2 x 2 x 2 = 16 (5 steps)
  • 16 + 1 = 17 (1 step)
  • 17 + 1 = 18 (1 step)
  • 18 x 2 = 36 (1 step)
  • 36 x 2 = 72 (1 step)
  • 72 + 1 + 1 = 74 (2 step)

This is my first attempt to reach 74. And here I have traversed to 74 in 11 steps.

Second Attempt:

  • 1 x 2 x 2 x2 = 8 (4 steps)
  • 8+1 = 9 (1 step)
  • 9 x 2 = 18 (1 step)
  • 18 x 2 = 36 (1 step)
  • 36 x 2 = 72 (1 step)
  • 72 + 1 + 1 = 74 (2 step)

Wow I have reduced one more step. So in this way I can reach in just 10 steps to reach 74 starting from 1 by using +1 and x2. And according to me it is the shortest way to reach 74. I have counted the initial value as well in the first step that is why the number of steps are 9 and if i do not count the initial value then the steps will become 8.

#include < iostream >
using namespace std;
int main() {
int number = 2; // Initial number
int steps = 0; // Counter for steps
number *= 2;
steps++;
number *= 2;
steps++;
cout << "After multiplying by 2 three times: " << number << " (Steps: " << steps << ")" << endl;
number += 1;
steps++;
cout << "After adding 1: " << number << " (Steps: " << steps << ")" << endl;
number *= 2;
steps++;
cout << "After multiplying by 2: " << number << " (Steps: " << steps << ")" << endl;
number *= 2;
steps++;
cout << "After multiplying by 2: " << number << " (Steps: " << steps << ")" << endl;
number *= 2;
steps++;
cout << "After multiplying by 2: " << number << " (Steps: " << steps << ")" << endl;
number += 1;
steps++;
number += 1;
steps++;
cout << "Final number: " << number << " (Total Steps: " << steps << ")" << endl;
return 0;
}

image.png

So in this program you can see all the steps It is how a program works and can count the steps. I have stored all the steps in the variable steps and the value of the variable steps is updated each time a step is completed.


3. Degree

Take your number, I take my 49 and add its digits and also add 7: 4+9+7=16
We need to write a program to raise the number to the 16th power - all of you will have different numbers.

I have taken my number which is 74. After adding 7 + 4 it becomes 11 and after adding 7 into it it becomes 18.

Here is the C++ program to perform the calculation of the 2^18 where 2 is the base and 18 is the power.

#include < iostream >
using namespace std;
int main()
{
int a = 2, pow; // 2^18
pow = a * a; // 2^2
pow = pow * pow; // 2^4
pow = pow * pow; // 2^8
pow = pow * pow; // 2^16
pow = pow * a; // 2^17
pow = pow * a; // 2^18
cout<<pow<<"\n"; // 262144
return 0;
}

So this program is raising the power of the base a which is equal 2 up to 18 in different steps. And the output of this program is 262144. So 2^18 = 262144.

image.png


Write a program to divide it into numbers. And now how to collect it from numbers back into a number? How to reverse a number. In other words, unfold. for example, how to get 39 from 93.

Here is the C++ program which splits the digits of a number and then reconstruct the digits to form the number. And the program will reverse the given number.

image.png

  • First of all in order to separate the digits of the number I used integer division / and module %.

  • Then in order to reconstruct the number I have multiplied the first digit with 10 and then I have added the second number to it.

  • In this way we can get the original number back.

  • After that in order to reverse the number I have changed the order of the numbers. Further you can see in the program.

  • So the program is working fine which is splitting the number into digits and then returning the same number again and at the end the program is reversing the order of the digits of the number as well.

Take the first three numbers from your profile (the number of followers or posts) - divide it into numbers.

#include < iostream >
using namespace std;
int main() {
int number = 538;
// Extract individual digits
int hundreds = number / 100; // 5
int tens = (number / 10) % 10; // 3
int units = number % 10; // 8
// Print the digits
cout << "The individual digits of " << number << " are:" << endl;
cout << "Hundreds: " << hundreds << endl;
cout << "Tens: " << tens << endl;
cout << "Units: " << units << endl;
return 0;
}
}

image.png

  • In the above program I have stored 538 in number variable

  • The hundreds, tens and units are extracted using the arithmetic operators.

  • Hundreds: number / 100

  • Tens: (number / 10) % 10

  • Units: number % 10

  • The the individual digits has been separated and printed on the screen.

Find a four-digit number in your profile (by writing out the first digits, or combining it with others, the first two of one and the first two of another.) Or a random number - the main thing is that it is not in other homework. Divide it into numbers - the first number is X, the second number is X, the third number is X, the fourth number is X

Seeing the condition to get the 4 digits number I have taken the number of the posts which is 4196. Here is the program which divides the number into its digits and then show the numbers in the form of the the first number is X, the second number is X, the third number is X, the fourth number is X.

#include < iostream >
using namespace std;
int main() {
int number = 4196;
// Extract individual digits
int first = number / 1000; // 4
int second = (number / 100) % 10; // 1
int third = (number / 10) % 10; // 9
int fourth = number % 10; // 6
// Print the digits
cout << "The individual digits of " << number << " are:" << endl;
cout << "First digit (X): " << first << endl;
cout << "Second digit (X): " << second << endl;
cout << "Third digit (X): " << third << endl;
cout << "Fourth digit (X): " << fourth << endl;
return 0;
}

image.png

  • The number 4196 has been stored in the number variable. The number has been converted into the digits using the arithmetic operations.

    • First digit: number / 1000
    • Second digit: (number / 100) % 10
    • Third digit: (number / 10) % 10
    • Fourth digit: number % 10

After performing the above calculation I have successfully gotten the 4 digits of the number these are given below:

  • The first number is = 4
  • The second number is = 1
  • The third number is = 9
  • The fourth number is = 6

I invite @enamul17, @saintkelvin and @walicted to join this contest to write the first code.

Sort:  
Loading...

image.png

Why are they all "the first"?
And who is walicted.

Hahaha it happened because of hurry I had to go somewhere but I wanted to publish my post first. But now updated everything. Thanks for the compliment.

It happens bro. I do that kind of mistake a lot. :D Good luck. I don't know programming, but I am interested in it. Do you have something to say about the relationship of Math and Programming?

Yes bro they are linked each other. Without math programming is nothing. In programming we have to build logic to solve the problems and it is where maths work.

I guess that's why I have almost no chances in programming, I have never been big fan of it. Haha.

So is it save to say that,basically, computer speaks a lot of Math?

Indeed, you know what computer reads and understand everything in maths, in binary language (0,1)

It's amazing what Programming achieve with only 0 and 1. Imagine one day when they are awake and taste the sweetness of 2.

Hahaha 🤣 they can't taste 2 because when we enter 2 computer converts it into 0,1 form and understand because it is the standard.

Upvoted! Thank you for supporting witness @jswit.

CONGRATULATIONS!!

Your post has been supported by TEAM SHINING STARS. We support quality posts, good comments anywhere, and any tags.


1000152665.gif

Curated by : @josepha