> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/sidef-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/sidef-lang/programming_tasks/c/carmichael_lambda_function.md).

# Carmichael lambda function

Built-in as **Number#carmichael\_lambda**:

```ruby
func iteratedToOne(n) {
    var k = 0
    while (n > 1) {
        n.carmichael_lambda!
        ++k
    }
    return k
}

say " n   λ   k"
say "----------"

for n in (1..25) {
    printf("%2d  %2d  %2d\n", n, n.carmichael_lambda, iteratedToOne(n))
}

say "\nIterations to 1       i     lambda(i)"
say "====================================="

var table = []

1..Inf -> each {|k|
    var n = iteratedToOne(k)
    if (!table[n]) {
        table[n] = true
        printf("%4s %18s %12s\n", n, k, k.carmichael_lambda)
        break if (n == 15)
    }
}
```

#### Output:

```
 n   λ   k
----------
 1   1   0
 2   1   1
 3   2   2
 4   2   2
 5   4   3
 6   2   2
 7   6   3
 8   2   2
 9   6   3
10   4   3
11  10   4
12   2   2
13  12   3
14   6   3
15   4   3
16   4   3
17  16   4
18   6   3
19  18   4
20   4   3
21   6   3
22  10   4
23  22   5
24   2   2
25  20   4

Iterations to 1       i     lambda(i)
=====================================
   0                  1            1
   1                  2            1
   2                  3            2
   3                  5            4
   4                 11           10
   5                 23           22
   6                 47           46
   7                283          282
   8                719          718
   9               1439         1438
  10               2879         2878
  11              34549        34548
  12             138197       138196
  13             531441       354294
  14            1594323      1062882
  15            4782969      3188646
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://trizen.gitbook.io/sidef-lang/programming_tasks/c/carmichael_lambda_function.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
