New answers tagged

2

Referential immutability You don't reassign your lists - targets and bases - so make them final. Type weakening So far as I can see, you aren't doing anything in your class that requires reference to the ArrayList methods of targets and bases. You should weaken them to Collection, or maybe List depending on some of your index-dependent loops. Also, omit ...


2

The provided implementation in the post is flawed and fails to port the Pari/GP code. In this statement Matrix([[2*x, -1], [1, 0]])*rem(1,x**r-1)*(1%n) the actual intentions of rem(1, x**r-1) and (1%n) are to create symbolic representations of % (x**r - 1) and % n for each matrix element, which is going to take effect during the computation of matrix ...


3

Implementation def xmat(r,n): return Matrix([[2*x, -1], [1, 0]])*rem(1,x**r-1)*(1%n) Am I correct in thinking that n will always be a positive integer greater than 2? If so, *rem(1,x**r-1)*(1%n) can be optimised away entirely. def myisprime(n): r=smallestr(n) if r==0: return n==2 else: xp=(xmat(r,n)**n)*Matrix([[x],[1]]) ...


6

Ooooh boy, a prime number finder with the question, "how do I make this faster." It's like asking a bartender what their favorite drink is: it really depends. However, before we get to performance, let's tackle some of the stylistic considerations in this code. Wrap the actual executing code in a if __name__ == '__main__' block. Follow standard naming ...


-3

here is how I did it in golang func is_prime(num int) int { // get the floor root of num floorRoot := (int(math.Floor(math.Sqrt(float64(num))))) //fmt.Println for i := 2; i < floorRoot; i++ { // if mode of num and i is 0 it is not prim if num%i == 0 { //return 0 fmt.Println("not prim") ...


1

1) Real implementation of RSA use the Chinese Remainder Theorem, which greatly improves the performance. 2) The big performance difference between encryption and decryption is a normal thing for RSA. It comes from the fact, that the performance of the modular exponentiation used depends on the number of 1 bits in the exponent. If you either chose the public ...


0

This program is an efficient one. I have added one more check-in if to get the square root of a number and check is it divisible or not if it's then its not a prime number. public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T; // number of test cases T = sc.nextInt(); long[] number = new ...


1

def sieve_eratosthenes(limit): if limit <= 1: return [] primes = [True] * limit for base in range(2, int(limit**0.5 + 1)): if primes[base]: primes[base * base::base] = [False] * ((limit - 1) // base - base + 1) primes[0] = primes[1] = False return list(compress(range(limit), primes)) No attempt at all to ...


1

I won't repeat the excellent comments made by the other answers on Prime Factorization, Dynamic Programming, Single Responsibility Principle, Indentation, f-Strings, Bugs, Naming, Documentation, and Separation of Input from Processing. Integer conversion After all those comments have been filtered out, the following programming style still jumps out and ...


3

Since you compute the primes between low and high to get the number of primes and you compute the primes between zero and low to check if your len(primes) is prime, why not compute primes from zero to high in one shot? This also has the big advantage of increasing the speed of your computing speed (yes, you read that right). Right now, to see if a number ...


3

You have a problem with indentation in this line: elif (x % i) == 1 and i == (x - 1): primes.append(x) f-strings: if int(len(primes)) in primes or int(len(primes)) in primes_2: print("Huh, fancy that!", len(primes), "is also also a prime number!") print("The prime numbers are: ",primes) you might want to replace this with f'strings that looks ...


3

Welcome to Code Review! Here are some suggestions. Naming Choose a better name than primes_2. I'm not clear on what this variable does. Write some documentation ...in triple quotes at the top of your function. Describe its inputs and outputs. Separate user input from processing Put your calculation code in a separate function from your user input and ...


0

Starting cross-out from size_t multiple = 2 * i is a serious pessimization. Every multiple of i with other factor less than i has already been crossed out during previous passes, which dealt with these smaller primes. You should start with size_t multiple = i * i. Notice that computing i * i eliminates the need to compute sqrt. The outer loop written as for ...


2

In general, we try to keep functions small and nice, but in your case, using so many functions is just weird. For example, are you sure the uncrossedIntegersUpTo function is needed at all? How about determineIterationLimit? You can just handle them in the main function. Also, std::vector<bool> is not like the normal vector. It is not a container ...


Top 50 recent answers are included