Message Boards Message Boards

GROUPS:

Add each number in the sequence to one value

Posted 1 month ago
594 Views
|
20 Replies
|
5 Total Likes
|

Good afternoon, Dear residents of Wolfram Community. I need to add each number in the sequence to one value.

Here is a formula that generates a Fibonacci sequence of up to 33 values in it.

fibonacci Sequence[n_] :=
Module[{prev = 0, fNext = 1, i = 0},
While[i++ < n, {fPrev, f Next} = {f Next, prev + fNext}];
fPrev]

Array[fibonacci Sequence, 33, 0]

{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, \
1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, \
196418, 317811, 514229, 832040, 1346269, 2178309}

and I need to add all the numbers that consists of more than 2 numbers, to one digit

for example : 377 > 3+7+7 = 17 > 1+7 = 8

And then, if possible, color all 123 = in red. 456 in green and 789 in blue

Thank you for your attention, I am waiting for an answer.

20 Replies
Posted 1 month ago

Try

list={0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,
 17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309};
crush[n_]:=Module[{m=n},If[m>=100,While[m>=10,m=Total[IntegerDigits[m]]]];m]
Map[crush,list]

which returns

{0,1,1,2,3,5,8,13,21,34,55,89,9,8,8,7,6,4,1,5,6,2,8,1,9,1,1,2,3,5,8,4,3}

Test this carefully and study the documentation for each of those functions used.

Posted 1 month ago

Ok, but what with 13, 21, 34, 55 ... ? It also needs to be added to one digit.

Posted 1 month ago

Your first message said "I need to add all the numbers that consists of more than 2 numbers, to one digit" so I took only the numbers that were three or more digits and crushed them to a single digit.

Do you understand how I read that now?

Can you figure out the code well enough to make a small change and have it crush numbers that are more than one digit?

Posted 1 month ago

I am new to all this formulas stuff and can't figure out how to make this small change. Could you please hellp me with this one ?

Posted 1 month ago

Replace

If[m>=100

with

If[m>=10
Posted 1 month ago

Oh, thank you so much. It is what i was looking for. May be you know how to color different set of numbers in different colors, such as 1,2,3 = in red. 4,5,6 in green and 7,8,9 in blue?

Posted 1 month ago

Hi Sergey,

A different approach compared to Bill's

data = FixedPoint[IntegerDigits /* Total, #] & /@ Table[Fibonacci[n], {n, 32}]
(* {1, 1, 2, 3, 5, 8, 4, 3, 7, 1, 8, 9, 8, 8, 7, 6, 4, 1, 5, 6, 2, 8, 1, 9, 1, 1, 2, 3, 5, 8, 4, 3} *)

Which[
   # <= 3,
   Style[#, Red],
   # <= 6,
   Style[#, Green],
   # <= 9,
   Style[#, Blue]] & /@ data

enter image description here

Posted 1 month ago

Nice. Does it possible to colorize digits based on different values like 147 - 258 - 369 ?

Posted 1 month ago

Sure. Why don't you try it (hint: MemberQ). If you get stuck, post the code you have tried.

Posted 1 month ago

There is a really simple way to compute this in O(1) time per integer. See this and this.

Mod[#, 9, 1] & @ Table[Fibonacci@n, {n, 32}]
(* {1, 1, 2, 3, 5, 8, 4, 3, 7, 1, 8, 9, 8, 8, 7, 6, 4, 1, 5, 6, 2, 8, 1, 9, 1, 1, 2, 3, 5, 8, 4, 3} *)
Posted 1 month ago

So, how to color sequence I get with values 147 = red; 258 = green; 369 = blue?

Posted 1 month ago

See my earlier response

Why don't you try it (hint: MemberQ). If you get stuck, post the code you have tried.

Posted 1 month ago

What does it mean hint: MemberQ ?

Posted 1 month ago

It means that if you are interested in using the Wolfram Language to solve your problems then you need to learn it. MemberQ.

Posted 1 month ago

Thank you for the hint. But i can't still figure out how to make this MemberQ work. Could you please correct me ?

data = FixedPoint[IntegerDigits /* Total, #] & /@ 
  Table[Fibonacci[n], {n, 32}]
(*{1,1,2,3,5,8,4,3,7,1,8,9,8,8,7,6,4,1,5,6,2,8,1,9,1,1,2,3,5,8,4,3}*)

Which[MemberQ[{1, 4, 7}[#, Red], # <= 
   MemberQ[{2, 5, 8}[#, Green], #MemberQ[{3, 6, 9}[#, Blue]] & /@ data
Posted 1 month ago

Take a look at the color coding of the Which expression

enter image description here

Notice that there are some red [ and # and #MemberQ in green. They highlighting syntax issues.

MemberQ expects two arguments MemberQ[{1, 4, 7}[#, Red] clearly does not provide two arguments. Learn how MemberQ works

MemberQ[{1, 4, 7}, 1]
(* True *)

MemberQ[{1, 4, 7}, 2]
(* False *)

If you want to use the operator form of MemberQ

MemberQ[1][{1, 4, 7}]

Read this and do all the exercises.

Posted 1 month ago

Hm, still no luck

data = FixedPoint[IntegerDigits /* Total, #] & /@ 
  Table[Fibonacci[n], {n, 32}]
(*{1,1,2,3,5,8,4,3,7,1,8,9,8,8,7,6,4,1,5,6,2,8,1,9,1,1,2,3,5,8,4,3}*)

Which[MemberQ[1][{1, 4, 7}[#, Red], # <= 
   MemberQ[2][{2, 5, 8}[#, 
     Green], # MemberQ[3][{3, 6, 9}[#, Blue]] & /@ data

Could you please show me the code, so i can see how it's done ?

Posted 1 month ago

It is unfair to expect other people to spend time helping you when you have not spent time to even fix the syntax errors in your code.

Posted 1 month ago

I am trying to fix it, but it's still not working properly. Could you point me where is an error in syntax ?

Posted 1 month ago

And also I need to set all this result in 6 row column.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract