Mathematica: Help with creating a more efficient algorithm

  • Context: Mathematica 
  • Thread starter Thread starter Smidgen
  • Start date Start date
  • Tags Tags
    Algorithm Mathematica
Click For Summary

Discussion Overview

The discussion revolves around creating a more efficient algorithm in Mathematica for counting primes that satisfy specific criteria. Participants explore different coding approaches and seek to refine the function to calculate the percentage of such primes up to a given number.

Discussion Character

  • Technical explanation, Mathematical reasoning, Exploratory

Main Points Raised

  • One participant presents an initial algorithm to count primes up to 1000 that meet certain multiplicative order conditions.
  • Another participant identifies a syntax error in the original code, suggesting the use of parentheses instead of brackets and noting that the Return statement is redundant.
  • A participant confirms that correcting the syntax leads to obtaining the correct percentage of primes.
  • Alternative implementations of the function are proposed, utilizing different Mathematica functions like Select and Count to achieve the same result.
  • One participant expresses appreciation for the variety of coding approaches presented in the discussion.

Areas of Agreement / Disagreement

Participants generally agree on the need for a more efficient algorithm and share different methods to achieve the same outcome. However, there is no explicit consensus on which method is superior.

Contextual Notes

Some limitations may include assumptions about the range of primes and the specific criteria used in the multiplicative order calculations, which are not fully detailed in the discussion.

Who May Find This Useful

Individuals interested in algorithm optimization in Mathematica, particularly those focused on prime number theory and mathematical programming.

Smidgen
Messages
3
Reaction score
0
I've created a simple algorithm to count primes up to say 1000 which satisfies a certain criterion :

count = 0;
Do[p = Prime[jj];
If[And[MultiplicativeOrder[2, p] == p - 1,
MultiplicativeOrder[3, p] == p - 1], count = count + 1], {jj, 3, 1000}]
count

Now this algorithm works... but I'm interested in the percentage of primes (here, out of 1000) that satisfies the criterion. My algorithm only counts the primes but does not divide the number of primes counted by the number of primes. I would like to create a function where I can input any x ( the number of primes), and the function will spit out the percentage that I'm interested in. But I'm having a problem with this function ( particularly with Return[] ) that I'm testing:

func23[x_] :=
[
count = 0;
Do[p = Prime[jj];
If[And[MultiplicativeOrder[2, p] == p - 1,
MultiplicativeOrder[3, p] == p - 1], count = count + 1], {jj, 3,
x}];
Return[count/(x - 2)]
]

Any insights or suggestions would be greatly appreciated :)
 
Physics news on Phys.org
The code you show has a syntax error. You need to replace the outermost [] with (). The Return is redundant, but not harmful.
 
DaleSpam, thanks for spotting it... I tried your suggestion and I was able to get the correct percentage I was looking for.
 
func23[x_] :=
Length[
Select[Range[3, Prime[x]],
PrimeQ[#] && MultiplicativeOrder[2, #] == #-1 && MultiplicativeOrder[3, #] == #-1 &
]
]/(x - 2)

OR

func23[x_] :=
Count[Range[3, Prime[x]], z_ /; PrimeQ[z] && MultiplicativeOrder[2, z] == z-1 && MultiplicativeOrder[3, z] == z-1]/(x - 2)
 
Last edited:
Bill Simpson, thanks for the additional codes... glad I could see different ways of arriving at the same percentage.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 10 ·
Replies
10
Views
1K