1

let n be an integer and A = {2,3,...,10} and I want to do as follows:

  1. divide n to 2, so there is a reminder r2 and a quotient q2.
  2. divide q2 to 3, so there is a reminder r3 and a quotient q3.
  3. we repeat this until the quotient is less than the next number.
  4. write together the last quotient with the previous reminders.

For example n=45

45/2 .......    r_2=1, q_2=22
22/3 .......    r_3=1, q_3=7
 7/4 .......    r_4=3, q_4=1

since q4 = 1 is less than the next number i.e. 5, we break.

the result is q4r4r3r2 where it is equal to 1311.

Thank you for your help.

I did this but it does not work

n = 45;
i = 2;
list = {Mod[n, i]};

While[Quotient[n, i] >= i + 1, n == Quotient[n, i]; i++; 
   AppendTo[list, Mod[n, i]];
   If[Quotient[n, i] < i + 1, Break[]]; AppendTo[list, Quotient[n, i]]];

list
Row[Reverse[list]]

which gives

{1, 0, 15, 1, 11, 0, 9, 3, 7, 3}
Row[{3, 7, 3, 9, 0, 11, 1, 15, 0, 1}]

where it is not my desired result.

asd
  • 337
  • 3
  • 5
  • 13
  • Please explain the context of your question. The way you've written it sounds too much as homework. Homework questions are OK, btw, but they deserve a "special" treatment (usually trying to teach fishing techniques, rather than fishing for you) – Dr. belisarius Oct 24 '12 at 03:03
  • 1
    Also, please try to include any code you were able to produce to solve the prolem – Dr. belisarius Oct 24 '12 at 03:06
  • For *mixed-radix arithmetic* this SO question -- http://stackoverflow.com/questions/759296/converting-a-decimal-to-a-mixed-radix-base-number -- provides some useful guidance. – High Performance Mark Oct 24 '12 at 11:59

2 Answers2

1

You might use something like this:

f[n_Integer] := 
 NestWhileList[
   {QuotientRemainder[#[[1, 1]], #[[2]] + 1], #[[2]] + 1} &,
   {{n}, 1},
   #[[1, 1]] != 0 &
 ] // Rest

f[45]
{{{22, 1}, 2}, {{7, 1}, 3}, {{1, 3}, 4}, {{0, 1}, 5}}

You can use Part to get whatever bits of the output you desire.

Here's a somewhat more advanced way if you can handle the syntax:

f2[n_Integer]    := Reap[f2[{n, 0}, 2]][[2, 1, 2 ;;]] // Reverse

f2[{q_, r_}, i_] := f2[Sow @ r; QuotientRemainder[q, i], i + 1]

f2[{0, r_}, i_]  := Sow @ r

f2[45]
{1, 3, 1, 1}
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • thanks, the second one works but 1)for limited numbers, 2) it is complicated for me. 3) I want to erase commas and bracket just i need an integer like 1311. if you can help me even with longer code but simpler. – asd Oct 24 '12 at 20:13
  • @asd (1) what do you mean *for limited numbers*? I just tried it on `10^1000` and it works fine. (2) This is why I also provided the first method. Do you understand it? If not I'll explain later, but I don't have time now. (3) Look at `FromDigits`. – Mr.Wizard Oct 24 '12 at 20:17
1

This is the code:

A = Table[i, {i, 2, 10}]; (* array of numbers *)
n = 45; (* initial value *)
ans = {}; (* future answer which is now empty list *)
For[i = 1, i <= Length[A], i++, (* looping over A *)
 If[n < A[[i]], (* exit condition *)
  ans = Append[ans, n]; (* appending last n when exit *)
  Break[]
  ];
 qr = QuotientRemainder[n, A[[i]]]; (* calculating both quotient and reminder *)
 ans = Append[ans, qr[[2]]]; (* adding second member to the answer *)
 Print[qr]; (* printing *)
 n = qr[[1]]; (* using first member as new n to process *)
 ];
ans (* printing result in Mathematica manner *)

It gives

{1, 1, 3, 1}
Dims
  • 47,675
  • 117
  • 331
  • 600