Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:50:33+00:00 2026-05-28T07:50:33+00:00

In Mathematica, do I have to use an explicit loop to calculate the product

  • 0

In Mathematica, do I have to use an explicit loop to calculate the product of elements in a given list (potentially very long) modulo to another number?

Please teach me your elegant approach if you do have. Thanks!

Edit

Just to give an example

list=Range[2000];Mod[Product[list],32327]

The above is very inefficient, because while calculating the products, one could have taken the modulo to make the multipliers smaller.

Edit 2

I guess my question relates to how to replace for loop for

Module[{ret = initial_value}, For[i = 1, i <= Length[list], i++, ret = general_function[list[[i]],ret]; ret]

given a general function general_function and a list list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:50:34+00:00Added an answer on May 28, 2026 at 7:50 am

    For long lists a divide-and-conquer is typically faster. The idea is to compute the times-mod for the first and second halves, multiply that, and take the mod.

    Here is an example. We’ll use a list of 10^6 integers, all between 0 and 10^10.

    SeedRandom[1111111];
    len = 6;
    max = 10;
    list = RandomInteger[10^max, 10^len];
    

    Multiplying and taking the modulus, for a slightly larger mod (I wanted to decrease the likelihood that the result was zero):

    In[119]:= Timing[Mod[Times @@ list, 32327541]]
    
    Out[119]= {1.360000, 8826597}
    

    Here is a variant of the sort I described. Trial and error tuning indicated that lists of length 2^9 or so were best done nonrecursively, at least for numbers in the size range indicated above.

    tmod2[ll_List, m_] := With[{len=Floor[Length[ll]/2]},
      If[len<=256,
        Mod[Times @@ ll, m],
        Mod[tmod2[Take[ll,len],m] * tmod2[Drop[ll,len],m], m]]]
    
    In[120]:= Timing[tmod2[list, 32327541]]
    
    Out[120]= {0.310000, 8826597}
    

    When I increase the list length to 10^7 and allow ints from 0 to 10^20, the first method takes 50 seconds and the second one takes 5 seconds. So clearly the scaling is working to our advantage.

    For situations where an iteration interleaving two operations might be preferred to divide-and-conquer, one might use Fold as below.

    tmod3[ll_List, m_] := Fold[Mod[#1*#2,m]&, First[ll], Rest[ll]]
    

    While not competitive with tmod2 on long lists, this is faster than multiplying out everything prior to invoking Mod. For length 10^7 and max element 0f 10^20 it takes around 8 seconds to do what tmod2 did in 5.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large table in Mathematica ((dimcub-1)^3 elements) coming from an inverse
In Mathematica I have a list: x = {1,2,3,3,4,5,5,6} How will I make a
I'm trying to drop some elements of a list of data in Mathematica, but
i have the following in mathematica and want to use it in matlab.I tried
I have this many hundreds of cell long Mathematica file and I want to
I am very new to Java, and trying to use Mathematica's Java interface to
Any easy question for the Mathematica experts here: Given a list, say Clear[a, b,
Is it possible to use Mathematica's manipulate to change variables that have already been
If you have Mathematica code in foo.m, Mathematica can be invoked with -noprompt and
I have a 15-day trial of Mathematica and need to evaluate it, and see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.