Python(Simple math codes)

This is a place to discuss python. Here are two codes I made to find out the prime factors.

  1 2 3 4 5 6 7 8 9 10 #NAME: Prime Factors 1 aka First n = int(input('Enter the number whose factors you need to know: ')) i = 2 while (n != 1): if (n%i == 0): print(i, n//i) n //= i i += 1 if (i>n): i = 2 

 1 2 3 4 5 6 #NAME: Prime Factors 2 aka Second n = int(input('Enter the number whose factors you need to know: ')) for i in range(2,n): if(n%i == 0): print(i, n//i) n//=i 

These may not be the best but are quite efficient. Share your code in the comments.

Also, make a program to find the HCF of two numbers(it probably requires the use of lists and I don't know much about lists).

3 weeks, 1 day ago

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

• Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
• Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
• Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold
- bulleted- list
• bulleted
• list
1. numbered2. list
1. numbered
2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in $$ ... $$ or $ ... $ to ensure proper formatting.
2 \times 3 $2 \times 3$
2^{34} $2^{34}$
a_{i-1} $a_{i-1}$
\frac{2}{3} $\frac{2}{3}$
\sqrt{2} $\sqrt{2}$
\sum_{i=1}^3 $\sum_{i=1}^3$
\sin \theta $\sin \theta$
\boxed{123} $\boxed{123}$

Sort by:

program to find the HCF of two numbers can be made using Euclid's algorithm

- 3 weeks, 1 day ago

It doesn't requires list but rather it requires recursion

- 3 weeks, 1 day ago

I've done it. A program to find the HCF of two numbers. It uses Euclid's Division Algorithm which is the same as finding the HCF using the long division method.

 1 2 3 4 5 6 7 8 9 #NAME: HCF r = 1 a = int(input('Enter the smaller number')) b = int(input('Enter the bigger number')) while (r>0): r = b%a b = a a = r print(b) 

- 3 weeks ago

You only need to check for factors upto $\sqrt{n}$ the factors over that are just $\frac{n}{\text{already known factors}}$, this can increase efficiency by quite a lot

- 2 weeks, 5 days ago

But $\sqrt{n}$ is float for most of the integers 'n' and the range loop is not accepting floats.

I reduced it to half by doing

 1 for i in range(2,n//2+1): 

- 2 weeks, 4 days ago

 1 for i in range(2,int(n**0.5+1)): 

- 2 weeks, 4 days ago

yes it works!

- 2 weeks, 4 days ago

Yeah. Try

for(n=2;n*n<=i,n++)
{
Code
}


- 2 weeks, 5 days ago

I don't understand what you want to say

- 2 weeks, 4 days ago

You need only check for numbers up to $\sqrt{n}$!

- 2 weeks, 4 days ago

can you(anyone) suggest anything so that it does powers easily. Like I tried $5^{10} = 9765625$ in both the prime factor programs and the output was

 1 2 3 4 5 Enter the number whose factors you need to know: 9765625 5 1953125 25 78125 125 625 625 1 

- 2 weeks, 4 days ago

Check out this code, gives all prime factors and their multiplicity too

  1 2 3 4 5 6 7 8 9 10 11 n=int(input()) i=2 prime=[] while i<=n**0.5: while n%i==0: prime.append(i) n/=i i+=1 if n!=1: prime.append(n) print(prime) 

- 2 weeks, 4 days ago

It is good. I added this at the last in place of print(prime) to print each factor in a separate line

 1 2 for n in prime: print(n) 

- 2 weeks, 4 days ago

@Jason Gomez Is there any function in python which finds the common items in two or more lists?

- 2 weeks, 3 days ago

Yes you can just use set conversion of both lists and then use intersection method to do so.

 1 2 3 list1 = [3,4,5] list2 = [3, 5, 7, 9] list(set(list1).intersection(list2)) 

 1 Output : [3,5] 

- 5 days, 20 hours ago

Can you explain how this works? And can we store the commons in a list?

- 5 days, 19 hours ago

Have you worked with sets in maths?

- 5 days, 17 hours ago

I know the basics, union and intersection.

- 5 days, 4 hours ago

This might help yu learning about intersection function in Python

- 5 days, 3 hours ago

If I run this, their is no output.

- 5 days, 4 hours ago

Well yu have to use print function to get the output

- 5 days, 3 hours ago

How can I store them in a list.

- 4 days, 23 hours ago

It just results as a list so why are you storing it?

- 4 days, 19 hours ago

I wanted to access the items later. This works

 1 2 3 list1 = [3,4,5] list2 = [3, 5, 7, 9] list3 = list(set(list1).intersection(list2)) 

- 2 days, 2 hours ago