Tag Info

New answers tagged

1

I had interpreted the question a bit differently. The word "iterative" in the question led me to think that OP might want to see the graphs connecting the happy numbers and unhappy numbers in Mathematica. Here's how to get the two graphs: nums = Table[NestWhileList[Composition[#.# &, IntegerDigits], k, (FreeQ @@ Through[{Most, ...


2

Just to ensure this one isn't going to engross the unanswered internal bag. As I said in a comment the following might work for two "iterations": base = List /@ Range@100; base1 = {#, Tr[IntegerDigits[#]^2]} & /@ base[[All, -1]]; base2 = {#, Tr[IntegerDigits[#]^2]} & /@ base1[[All, -1]]; DirectedEdge @@@ Union[base1, base2]; Graph[%, GraphLayout ...


2

Here is a quick rewrite of your code. CipherSolve[modulus_, b_] := Module[{y = b, yList = {}, m = Ceiling[Sqrt[modulus]], pmod, modinv, z}, modinv = PowerMod[10^m, -1, modulus]; pmod = PowerMod[10, Range[0, m - 1], modulus]; While[FreeQ[pmod, y], yList = Append[yList, y]; y = Mod[y*modinv, modulus] ]; z = ...


47

If you want to make several sequences of the Collatz function for turning it into a graph, you probably want to memorize, which parts you already calculated. What we try to do is to create a graph like this (image from xkcd) When we would calculate the whole chain for each number until it (hopefully) reaches the end sequence 8,4,1 we do a lot of work over ...


4

The fastest way is probably a compiled sieve of Erostaneses. PrimesUpTo = Compile[{{n, _Integer}}, Block[{S = Range[2, n]}, Do[ If[S[[i]] != 0, Do[ S[[k]] = 0, {k, 2i+1, n-1, i+1} ] ], {i, Sqrt[n]} ]; Select[S, Positive] ], CompilationTarget -> "C", RuntimeOptions -> "Speed" ]; ...


28

This is the Collatz function I know: Collatz[1] := {1} Collatz[n_Integer] := Prepend[Collatz[3 n + 1], n] /; OddQ[n] && n > 0 Collatz[n_Integer] := Prepend[Collatz[n/2], n] /; EvenQ[n] && n > 0 Generating a graph from this is easy: Graph[(DirectedEdge @@@ Partition[Collatz[#], 2, 1]) & /@ Range[500] // Flatten // Union, ...



Top 50 recent answers are included