SEC-S20W3 Writing the first code
Hello steemians,
Programming has always fascinated me with its power to transform ideas into concrete solutions. By participating in this contest for the third week of Season 20 of the Steemit Engagement Challenge, I recall my beginnings with code. My first encounter with programming was not only a technical challenge, but also an enriching learning journey. This contest allows me to revisit this adventure and share my first coding experiences, reflecting on the importance of the first lines of code in the formation of a programmer.
Choose your programming environment. Find and show 1) the code entry window, 2) the output window 3) the RUN button and 4) the data entry window - if you can't find this item, don't worry, I'll show you.
I choose to use the OnlineGDB programming environment for several reasons. First, this environment allows me to write and run code directly online without requiring a local installation. I have easy access to the code entry window (1), where I can type and edit my program. Second, the output window (2) immediately shows me the results of the code execution, which is essential for debugging and testing my programs.
I can also use the RUN button (3) to launch the program and instantly see the results in the output window. While the data entry window (4) is not always necessary for every program, it becomes useful when I want to test interactive programs that require user input. This environment is flexible and supports multiple programming languages, allowing me to switch between them easily.
I chose this environment because of its simplicity and online availability, which allows me to access my projects from any device connected to the Internet, while working in a user-friendly and intuitive environment.
find your way to act, від 1 до 100effective or not in this task is not important.
1x2=2x2=4+1+1=6x2=12x2=24+1=25x2=50x2=100
I start with 1 and aim to reach 100 using as few steps as possible with the +1 or x2 operations. My goal is to increase values quickly while making adjustments with the +1 operation where necessary to reach strategic points that can be efficiently doubled.
I start by multiplying 1 by 2, which gives me 2. I choose this first multiplication to speed up the progression to larger values. Then, I multiply by 2 again to get 4. At this point, I have already made good progress with two successive multiplications.
Rather than continuing directly with the multiplication, I add 1 to 4 to get 5. This allows me to get closer to a value that is easier to double later. I repeat this operation a second time, adding another 1, to reach 6. The choice of these two consecutive additions is strategic: starting at 6, I can now use the multiplication by 2 to get much larger values.
I then multiply 6 by 2, which gives me 12. Multiplication allows for a significant jump, and by doubling this value, I quickly reach 24. Here, I add 1 again to reach 25, a key number since doubling it immediately brings me closer to the final goal.
I then multiply 25 by 2 to reach 50, then double this value one last time, which allows me to arrive at exactly 100. Throughout the process, I alternated between multiplications to maximize gains in values and additions of 1 to adjust my numbers, in order to stay on an optimal trajectory.
find the best, shortest way. No one found it in the last lesson. Search!
Today I found an efficient solution to reach 100 starting from 1, using only the operations +1 and x2. In the last lesson, no one had found this method, but after some careful thought, I discovered a way to optimize the number of steps.
The key to my solution is the thoughtful alternation between multiplying by 2 and adding by 1, which allows me to maximize the progress towards 100 while making strategic adjustments quickly. By doubling at the right time and adding 1 where necessary, I was able to minimize the steps.
I encourage you to look for and try this approach because it has not been found before. It is not only fast but also clever, and it shows the importance of understanding how simple operations can combine to form an efficient solution.
if in addition to +1and x2it is possible to create an additional variable int k;, how many steps will it save, you cannotk=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.
If I add an additional variable int k
, this opens up new possibilities to optimize the process and reduce the number of steps. However, there is one important constraint: I cannot directly assign a specific value to k
(like 7 or 23). I can only fix the value of k
at a given step of my calculation.
The idea would be to use this variable k
to store an important intermediate value that I could reuse later, without having to recalculate this value. For example, after reaching a key value, I could fix k
to this value and use it to speed up subsequent calculations.
Let's take an example to see how many steps this could save. If I fix k
at a strategic step, say when I reach 25, I could store this value in k
and reuse it later. Rather than starting from 1 or recalculating some steps, I could simply double k
or add k
to another value.
In terms of step savings, adding this variable could save several steps, especially in situations where intermediate values are reused. This could reduce the total number of steps by 1 to 3, depending on how k
is used and at what step it is set.
Go to your profile from the number of subscribers, take the first two numbers, mine is 28 (out of 285). Add 21 to it. 28+21=49. This is your option for the task. Remember him!!! That is, you should reach from 1 to 49 (all of you will have your own, different numbers) by performing only operations +1andx2
In my case, 30+21=51
I start by multiplying 1 by 2, which gives me 2. Then I multiply by 2 again to get 4. At this point, I add 1 to get 5, then add 1 more to get 6. I then decide to multiply by 2 to get 12, and continue with another multiplication by 2 to get 24.
Then I add 1 to get 25. I multiply 25 by 2 to get 50. Finally, I add 1 to get the final result of 51.
Using this method, I used the operations optimally to get to 51 in 9 steps.
Here is a version of the algorithm in Python without using a list, where each step is simply displayed:
Take your number, I take my 49 and add its digits and 7: 4+9+7=16. We need to write a program to raise a number to the 16th power - all of you will have different numbers.
In my case, I take my 51 and add its digits and also add 7: 5+1+1=13
I start by initializing the variable a
to 5, which is the base I want to raise to the power of 13. I start by multiplying it by itself, which gives me ( 5^2 ). Then, I multiply this result by 5, which allows me to obtain ( 5^3 ).
At this point, rather than continuing with repeated multiplications, I adopt a more efficient approach. I take the result of ( 5^3 ) and multiply it by itself, which directly gives me ( 5^6 ), thus saving steps.
I continue by multiplying ( 5^6 ) by itself to obtain ( 5^{12} ). Finally, I multiply this result by the base (5), which allows me to arrive at ( 5^{13} ). This method allows me to quickly reach the power of 13 by optimizing the number of operations.
If you want to change the base to ( a = 5 ) and raise it to the 13th power, here’s the updated Python code:
1 point 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’s a simpler Python program that splits 51 into tens and units, then recomposes and reverses the number using only tens and units:
Splitting and recomposing the number 51
# Number to split
n = 51
# Get tens and units
tens = n // 10
units = n % 10
print(f"Tens: {tens}, Units: {units}")
# Recompose the number
reconstructed_number = tens * 10 + units
print(f"Recomposed number: {reconstructed_number}")
Reversing a number (example with 51)
# Number to reverse
n = 51
# Get tens and units
tens = n // 10
units = n % 10
# Reverse the tens and units
reversed_number = units * 10 + tens
print(f"Reversed number: {reversed_number}")
Explanation:
- Splitting the number: The program uses integer division (
//
) to get the tens and modulo (%
) to get the units. - Recomposing the number: You multiply the tens by 10 and add the units to recompose the number.
- Reversing the number: To reverse the digits, you multiply the units by 10 and add the tens, effectively switching their positions.
Take the first three numbers from your profile (the number of followers or posts) - divide it into numbers.
To divide the number 304 into its digits (hundreds, tens, and units), you can use the following Python program:
# Number to divide
n = 304
# Get hundreds, tens, and units
hundreds = n // 100
tens = (n % 100) // 10
units = n % 10
print(f"Hundreds: {hundreds}, Tens: {tens}, Units: {units}")
Explanation:
- Hundreds: We get the hundreds place by performing integer division of the number by 100.
- Tens: To get the tens place, we first remove the hundreds (
n % 100
) and then divide the result by 10. - Units: The units place is obtained by taking the remainder of the number when divided by 10 (
n % 10
).
For the number 304, the output will be:
- Hundreds: 3
- Tens: 0
- Units: 4
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
Here’s the Python program that divides the number 3048 into its digits and assigns each digit to X
:
# Number to divide
n = 3048
# Get each digit
X1 = n // 1000 # First digit (thousands)
X2 = (n % 1000) // 100 # Second digit (hundreds)
X3 = (n % 100) // 10 # Third digit (tens)
X4 = n % 10 # Fourth digit (units)
# Print the result
print(f"X1 = {X1}, X2 = {X2}, X3 = {X3}, X4 = {X4}")
Explanation:
- X1: The first digit is obtained by dividing the number by 1000 (
n // 1000
), which gives the thousands place. - X2: The second digit is obtained by removing the thousands and then dividing by 100 (
(n % 1000) // 100
), which gives the hundreds place. - X3: The third digit is obtained by removing the hundreds and dividing by 10 (
(n % 100) // 10
), which gives the tens place. - X4: The fourth digit is obtained by taking the remainder of the number when divided by 10 (
n % 10
), which gives the units place.
For 3048, the output will be:
- X1 = 3
- X2 = 0
- X3 = 4
- X4 = 8
Thank you very much for reading, it's time to invite my friends @cruzamilcar63, @mvchacin, @adeljose to participate in this contest.
Best Regards,
@kouba01
I congratulate you for the presentation of your task on a very complex subject, well for me it is the programming, if I would love to master much more this subject.
Thank you for the invitation.
Blessings.
TEAM 4
Congratulations! Your post has been upvoted through steemcurator06. Good post here should be..