Mathematica: Help with creating a more efficient algorithm

In summary, the conversation discusses creating a simple algorithm to count primes up to a certain number that satisfies a specific criterion. The algorithm was successful, but the person is now interested in finding the percentage of primes that meet the criterion out of the total number of primes counted. They have attempted to create a function for this, but are having trouble with the syntax and have asked for suggestions. Two possible solutions were provided.
  • #1
Smidgen
3
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
  • #2
The code you show has a syntax error. You need to replace the outermost [] with (). The Return is redundant, but not harmful.
 
  • #3
DaleSpam, thanks for spotting it... I tried your suggestion and I was able to get the correct percentage I was looking for.
 
  • #4
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:
  • #5
Bill Simpson, thanks for the additional codes... glad I could see different ways of arriving at the same percentage.
 

Related to Mathematica: Help with creating a more efficient algorithm

1. What is Mathematica and how can it help with creating efficient algorithms?

Mathematica is a high-level programming language and computational software used in scientific, engineering, and mathematical fields. It provides built-in functions and algorithms that are optimized for efficiency, making it a powerful tool for creating efficient algorithms.

2. Can Mathematica be used for both numerical and symbolic computations?

Yes, Mathematica can handle both numerical and symbolic computations. It has a wide range of built-in functions for performing various mathematical operations, from basic arithmetic to complex mathematical functions.

3. How can I use Mathematica to optimize an existing algorithm?

Mathematica has a built-in function called "FindMinimum" that allows you to find the minimum value of a function, which can be useful for optimizing an algorithm. Additionally, you can use the "Compile" function to compile your code for better performance.

4. Are there any resources available for learning how to use Mathematica for creating efficient algorithms?

Yes, there are many online tutorials, guides, and documentation available for learning how to use Mathematica for creating efficient algorithms. The official Mathematica website also provides several resources, including a user forum and online training courses.

5. Can Mathematica be used for parallel computing to further improve algorithm efficiency?

Yes, Mathematica has built-in support for parallel computing, allowing you to distribute your computations across multiple processors or even multiple computers. This can significantly improve the efficiency of your algorithm by reducing computation time.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
313
  • Programming and Computer Science
Replies
10
Views
827
  • Programming and Computer Science
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
535
  • Engineering and Comp Sci Homework Help
Replies
1
Views
611
Replies
1
Views
849
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top