prime-factoring

Prime factorization of a big number

时光毁灭记忆、已成空白 提交于 2020-02-05 06:06:04
问题 I'm trying to create a program for prime factorization of a number and this is the code I came up with. def primeFactors(n): l=[] ss=0 for i in range(2,n,1): #checking for prime t=0 for j in range(2,i): if(i==2): continue if(i%j==0): t=t+1 if(t>0): continue else: if(n==0): break else: print(i) if(n%i==0): n=n//i ss=ss+1 i=i-1 if(n%i!=0 and ss>0): l.append(i) l.append(ss) ss=0 else: continue q="" for i in range(0,len(l),2): q=q+"("+str(l[i])+"**"+str(l[i+1])+")" return q The working of the

prime number python for loops

人走茶凉 提交于 2020-01-15 14:27:12
问题 Question: A program that take a positive integer n as input and returns True if n is a prime number, otherwise returns False. My Answer: n = int(input("Enter a number: ")) for i in range(2,n): if n%i == 0: print(False) print(True) when I enter a prime number it works but when I enter a non prime number it doesn't work. Example: >>> Enter a number: 12 False False False False True >>> please help! 回答1: You can break and use else : n = int(input("Enter a number: ")) for i in range(2, n): if n %

O(N*log(log(N))) algorithm for Codility's Peaks?

点点圈 提交于 2020-01-15 11:24:52
问题 The task description is here: https://codility.com/demo/take-sample-test/peaks It's also here: Codility Peaks Complexity First, I tried solving this myself but was only able to come up with what I believed to be a brute force solution. However, it scored 100/100: https://codility.com/demo/results/demoRNWC3P-X4U Which obviously is completely unsatisfying for me. ;) The outer loop is called for each factor of N (or for each peak, whichever is smaller) and the inner one is called for each peak

Super Ugly Number

五迷三道 提交于 2020-01-13 05:25:27
问题 So the problem is: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4. So my algorithm basically finds all possible factors using the pattern they follow, pushes them to an array, sorts that array and then returns the nth value in the

Explain a code to check primality based on Fermat's little theorem

孤街醉人 提交于 2020-01-01 11:58:49
问题 I found some Python code that claims checking primality based on Fermat's little theorem: def CheckIfProbablyPrime(x): return (2 << x - 2) % x == 1 My questions: How does it work? What's its relation to Fermat's little theorem? How accurate is this method? If it's not accurate, what's the advantage of using it? I found it here. 回答1: 1. How does it work? Fermat's little theorem says that if a number x is prime, then for any integer a : If we divide both sides by a , then we can re-write the

Sieve of Eratosthenes - prime factor

拟墨画扇 提交于 2019-12-25 08:35:37
问题 I just wrote the following to utilize the sieve to find the largest prime factor of some natural number greater than 2. The program builds and runs and works for small test values, but simply crashes for values larger than, say, 1000000. I wrote this myself--and believe it will probably be extremely inefficient--after finding out what the sieve is about. Can you please suggest improvements? Thank you. //LARGEST PRIME FACTOR w/SIEVE OF ERATHOSTHENES #include <iostream> #include <math.h> using

Prime factorization for big numbers

做~自己de王妃 提交于 2019-12-23 21:33:34
问题 I am trying to find the complexity of a factorization for big numbers. Which is the best algorithm and which is the complexity of finding a number's prime factors? Assume that the length of the number is n. 回答1: The best know algoritm for factoring integer larger than 100 digits is General number field sieve. It's complexity is explained on the page that the link links to. Wikipedia has a nice article about other algoritms: http://en.wikipedia.org/wiki/Integer_factorization 回答2: the

Returning String of prime factors in Java

别来无恙 提交于 2019-12-23 12:38:06
问题 I know this is a classic problem. I solved it in Java. I have my solution below. However, when I used this solution in codefights.com, It went beyond the execution time limit. I would appreciate if anyone could give me suggestions on improving this code in any way possible. Please feel free to criticize my code so that I can improve on my coding skills. Thanks You're given number n. Return n as a product of its prime factors. Example For n = 22 the output should be "2*11". For n = 120 the

Project Euler 3 - Why does this method work?

旧城冷巷雨未停 提交于 2019-12-21 17:32:41
问题 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? I solved this problem on Project Euler my own way, which was slow, and then I found this solution on someone's github account. I can't figure out why it works. Why are a number of factors removed, equal to an index? Any insight? def Euler3(n=600851475143): for i in range(2,100000): while n % i == 0: n //= i if n == 1 or n == i: return i 回答1: This function works by finding successive

Python - Integer Factorization into Primes [closed]

♀尐吖头ヾ 提交于 2019-12-21 17:15:39
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I wrote an integer factorization function, but after messing around with it, I realized it had problems with a few numbers... >>> pFactors(99) # it does work for numbers with multiple of one prime factor [3, 3, 11] >>> pFactors(999) # however, sometimes, it doesn't [3, 37] # the actual prime factorization of 999