prime-factoring

Euler 3 Python. Putting the prime numbers into a list

梦想的初衷 提交于 2019-12-13 07:43:12
问题 Im still pretty new to python and I'm trying to get all of the prime numbers from 600851475143 into a list. However, I keep getting a random assortment of numbers in the list instead of the prime numbers. I'm not really sure where I am going wrong. Thank you for your time import math factors_list = [] prime_factors = [] def number_factors(s): s = int(math.sqrt(s)) for num in range(2, s): for i in range(2, num): if (num % i) == 0: factors_list.append(num) else: prime_factors.append(num) number

Largest prime factor of a number in Java

↘锁芯ラ 提交于 2019-12-12 18:51:17
问题 I am trying to find the Largest prime factor of a number while solving this problem here. I think that I am doing everything right, however one of the test case (#2) is failing and I can't think of any corner case where it might fail. Here's my code, please have a look and try to spot something. public class ProblemThree { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for (int i = 0; i < T; i++) { System.out.println(largestPrime

Find total count of Distinct Prime in given array using Python

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 18:51:54
问题 The problem is like this - You have given an array A having N integers. Let say G is the product of all elements of A. You have to find the number of distinct prime divisors of G. Example - Input : A = 1, 2, 3, 4 Output : 2 Explanation : Here g = 1*2*3*4 and distinct prime divisor of g are 2 and 3 To total count of distinct prime divisor = 2 Below is the code i wrote but the output which i am getting is wrong - class prime: # @param A : list of integers # @return an integer def prime(self,num

JavaScript: Function not returning largest prime factor

泄露秘密 提交于 2019-12-11 18:05:47
问题 Input: 13195 Expected Result: 29 (largest prime factor of input) Actual Result: 2639 (largest factor of input, but not a prime number) I didn't bother with even numbers because the largest prime will either be 2 or some odd prime multiplied by 2 to get the input, so what's the point. function findPrimeFactor(num) { let prime; for (let factor = 3; factor < num; factor += 2) { if (num % factor === 0) { for (let i = 3; i < factor; i += 2) { if (factor % i === 0) { break; } else { prime = factor;

Largest Prime Factor algorithm optimization

徘徊边缘 提交于 2019-12-11 12:18:28
问题 I'm trying to improve this interesting algorithm as much as I can. For now, I have this: using System; class Program { static void Main() { ulong num, largest_pFact; uint i = 2; string strNum; Console.Write("Enter number: "); strNum = Console.ReadLine(); num = ulong.Parse(strNum); largest_pFact = num; while (i < Math.Sqrt((double) largest_pFact)) { if (i % 2 != 0 | i == 2) { if (largest_pFact % i == 0) largest_pFact /= i; } i++; } Console.WriteLine("Largest prime factor of {0} is: {1}", num,

Powershell Multithreaded math

倾然丶 夕夏残阳落幕 提交于 2019-12-11 10:33:50
问题 I'm currently working on a self-inspired project to learn powershell, and have been writing a script to generate prime numbers. As it stands, the script works without issue, but my next goal is to increase it's processing speed. cls $Primes = @() $Primes += 3 $TargetNum = 5 $PrimesIndex = 0 $NumOfPrime = 3 while(1) { if(($TargetNum / 3) -lt 3) { $Primes += $TargetNum $TargetNum += 2 $NumOfPrime += 1 } else { if($Primes[$PrimesIndex] -le ($TargetNum / ($Primes[$PrimesIndex]))) { if($TargetNum

My Haskell Solution to Euler #3 is Inefficient

爱⌒轻易说出口 提交于 2019-12-11 06:37:41
问题 I am attempting to solve Euler problem 3 in Haskell, which involves finding the largest prime factor of a number. My code runs for a long time and seems to hang. What is causing my code to be so grossly inefficient? primes = sieve (2:[3,5..]) where sieve (x:xs) = x:[y | y <- (sieve xs), mod y x /= 0] sieve [] = [] primefactors n = filter (\x -> mod n x == 0) (primesUnder n) where primesUnder z = reverse (takeWhile (< z) primes) solve3 = head (primefactors 600851475143) 回答1: Your main problem

Prime numbers using Sieve of Atkin with BigInteger

≡放荡痞女 提交于 2019-12-11 03:39:12
问题 Does anyone happen to know of a C# Sieve of Atkin algorithm using BigInteger? From my understanding, this is the best known prime factorization algorithm currently in existence. I currently have this function: /// <summary> /// Finds prime numbers using the Sieve of Atkins algorithm. /// </summary> /// <param name="max">The limit of the prime list.</param> /// <returns>The list of prime numbers.</returns> public List<int> FindPrimes(int max) { var isPrime = new bool[max + 1]; var sqrt = (int)

finding greatest prime factor using recursion in c

可紊 提交于 2019-12-11 01:53:30
问题 have wrote the code for what i see to be a good algorithm for finding the greatest prime factor for a large number using recursion. My program crashes with any number greater than 4 assigned to the variable huge_number though. I am not good with recursion and the assignment does not allow any sort of loop. #include <stdio.h> long long prime_factor(int n, long long huge_number); int main (void) { int n = 2; long long huge_number = 60085147514; long long largest_prime = 0; largest_prime = prime

The largest prime factor with php

ぐ巨炮叔叔 提交于 2019-12-10 23:13:05
问题 I wrote a program in PHP to find the largest prime factor. I think it is quite optimized, because it loads quite fast. But, there is a problem: it doesn't count the prime factors of very big numbers. Here is the program: function is_even($s) { $sk_sum = 0; for($i = 1; $i <= $s; $i++) { if($s % $i == 0) { $sk_sum++; } } if($sk_sum == 2) { return true; } } $x = 600851475143; $i = 2; //x is number while($i <= $x) { if($x % $i == 0) { if(is_even($i)) { $sk = $i; $x = $x / $i; } } $i++; } echo $sk