Click here to Skip to main content
15,919,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am attaching my codes with the question. I have used the logic :
I have first found all the prime numbers in the 10 consecutive numbers starting from 1. Used them to divide each number in the range of the 10 consecutive numbers starting from 1. Then i have counted their power and then have displayed the product.

I know that I am wrong at the last step but I want to know whether I can use my initial logic to solve this problem. Other logics are equally welcomed. Thank You in advance.


What I have tried:

Python
count = 0
product = 1
num = 0
c1 = 0
for i in range(1,10):
    i += 1
    for n in range(2,i):
        num = i%n
        n += 1
        if num == 0:
            count += 1
        else:
            continue
    if count == 0:
            prime = i
    else:
            continue
    for x in range(2,10):
            if x%prime == 0:
                while x%prime==0:
                    c1 += 1
                product *= (prime**c1)
print(product)
Posted
Updated 2-Jul-19 9:08am
Comments
Richard MacCutchan 2-Jul-19 3:15am    
This is mathematics not programming. Google for LCM and you will find many explanations.
Member 14517556 2-Jul-19 6:06am    
I am quite familiar with lcm and can easily find it by paper pen But I am not able to figure out how to do the same with python
Richard MacCutchan 2-Jul-19 6:47am    
It is exactly the same issue; finding values and comparing them. As I suggested in your other question, your time would be better spent actually studying the python language.

Quote:
I am quite familiar with lcm and can easily find it by paper pen But I am not able to figure out how to do the same with python

If you are familiar with lcm, why do you stick with the most complicated algorithm?
Using the GCD is much easier and don't need prime factorization, which is a great improvement for large numbers.
Least common multiple - Wikipedia[^]

As far as I can see, this loop never ends (I am not using Python, but the logic is suspicious to me):
Python
while x%prime==0:
    c1 += 1

Quote:
I know that I am wrong at the last step

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Member 14517556 2-Jul-19 22:53pm    
Thank You
Maciej Los 3-Jul-19 15:05pm    
Well explained!
Patrice T 3-Jul-19 15:10pm    
Thank you
You have to decompose the numbers in factors and then multiply every found factor at its maximum power. See Factors of an integer - Rosetta Code - Python[^].
 
Share this answer
 
v4
Comments
Member 14517556 2-Jul-19 0:13am    
can you explain in a little detail as I am not getting it. What the last line is about?
CPallini 2-Jul-19 2:32am    
There was a mistake, in my answer. Sorry for the inconvenience. Fixed now.
Member 14517556 2-Jul-19 6:09am    
If you are comfortable can you please write it in a little detail. Actually I am doing the same or probably trying to do so but not getting the favourable result.
CPallini 2-Jul-19 11:25am    
See my updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900