# Question on building a variable with multiple math operations

Greg Perry <>
Fri Jul 10 17:17:39 CEST 2009

```I have a unique challenge that I can't seem to find a right way to do based on the immutability of Erlang.

I have built a unique factoring application with Erlang, which first analyzes any given N and depending on the properties of that subprime number a list of coefficients are assigned to a list (the list contains tuples actually).

http://blog.liveammo.com/2009/06/factoring-fun/

For example:

\$ erl
Erlang R13B01 (erts-5.7.2) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.2  (abort with ^G)
1> c("reductionsieve.erl").
{ok,reductionsieve}
2> c("lin.erl").
{ok,lin}
3> N1 = reductionsieve:make_prime(5) * reductionsieve:make_prime(5).
Generating a 5 digit prime ......
Generating a 5 digit prime ..................
823221601
4> reductionsieve:analyze(N1).
[{1,7},{2,8},{4,4},{5,5}]
5> N2 = reductionsieve:make_prime(5) * reductionsieve:make_prime(5).
Generating a 5 digit prime .
Generating a 5 digit prime ....
3193425841
6> reductionsieve:analyze(N2).
[{1,4},{2,2},{5,8},{7,7}]
7> N3 = reductionsieve:make_prime(5) * reductionsieve:make_prime(5).
Generating a 5 digit prime ....
Generating a 5 digit prime .
5575777817
8> reductionsieve:analyze(N3).
[{1,5},{2,7},{4,8}]

The list filled with tuples that returns is variable in length, depending on the properties of any given N.  I would like to be able to build a variable that takes each tuple from a list and multiplies them together, then adds them to the other products.  For example, if N yields properties of [{1,5},{2,7},{4,8}], I need to be able to build a variable which is: (1 * 5) + (2 * 7) + (4 * 8); some N will return a list as small as three tuples, other N will return a list that may have as many as nine tuples that need to be multiplied in that fashion.

I have been experimenting with lists:fold1 but it seems that is expecting a simple list without the use of tuples.

Any ideas on this one?