Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements function that returns the sum of a number's digits #28100

Open
sheareralexj mannequin opened this issue Jul 2, 2019 · 16 comments · May be fixed by #39665
Open

Implements function that returns the sum of a number's digits #28100

sheareralexj mannequin opened this issue Jul 2, 2019 · 16 comments · May be fixed by #39665

Comments

@sheareralexj
Copy link
Mannequin

sheareralexj mannequin commented Jul 2, 2019

This ticket adds a function to return, given a number and base, the sum of digits of that number in that base. For example, this enables:

sage: digit_sum(4412, b=10)
11
sage: digit_sum(4412, b=6)
12

Component: number theory

Author: Alex Shearer

Branch/Commit: u/gh-sheareralexj/implements_function_that_returns_the_sum_of_a_number_s_digits @ a8e7f14

Issue created by migration from https://trac.sagemath.org/ticket/28100

@sheareralexj sheareralexj mannequin added this to the sage-8.9 milestone Jul 2, 2019
@sheareralexj
Copy link
Mannequin Author

sheareralexj mannequin commented Jul 2, 2019

@sheareralexj
Copy link
Mannequin Author

sheareralexj mannequin commented Jul 2, 2019

New commits:

a8e7f1428100: Implements function that returns sum of digits of `n`

@sheareralexj
Copy link
Mannequin Author

sheareralexj mannequin commented Jul 2, 2019

Author: Alex Shearer

@sheareralexj
Copy link
Mannequin Author

sheareralexj mannequin commented Jul 2, 2019

Commit: a8e7f14

@sheareralexj sheareralexj mannequin added the s: needs review label Jul 2, 2019
@videlec
Copy link
Contributor

videlec commented Jul 3, 2019

comment:3

What is the advantage of this function compared to

sage: sum(n.digits(b))

?

@sheareralexj
Copy link
Mannequin Author

sheareralexj mannequin commented Jul 3, 2019

comment:4

Replying to @videlec:

What is the advantage of this function compared to

sage: sum(n.digits(b))

?

There is no real advantage, although we thought it makes sense to have a dedicated sum of digits function implemented.

@kedlaya
Copy link
Contributor

kedlaya commented Dec 19, 2019

comment:5

While we are at it, since digits is a method of Sage integers rather than a top-level function, maybe this should be also?

As an aside, it would have been more Pythonic if once upon a time, digits had been written to return an iterator rather than a list. But that would be a painful change to make now.

@jhpalmieri
Copy link
Member

comment:6

Replying to @videlec:

What is the advantage of this function compared to

sage: sum(n.digits(b))

?

A related question: what are the disadvantages to the proposed function digit_sum? I don't know if this function is of enough general interest to warrant being defined at the top level, rather than being a method for integers and/or a function which has to be imported if you want to use it. There have been some efforts to remove underused items from the top-level name space (#25383), and I think there should be a good reason for adding a new one.

tl;dr: why not instead implement 4412.digit_sum(b=10)?

@embray
Copy link
Contributor

embray commented Dec 30, 2019

comment:7

Ticket retargeted after milestone closed

@embray embray modified the milestones: sage-8.9, sage-9.1 Dec 30, 2019
@jplab
Copy link
Contributor

jplab commented Feb 14, 2020

comment:8

Replying to @jhpalmieri:

Replying to @videlec:

What is the advantage of this function compared to

sage: sum(n.digits(b))

?

A related question: what are the disadvantages to the proposed function digit_sum? I don't know if this function is of enough general interest to warrant being defined at the top level, rather than being a method for integers and/or a function which has to be imported if you want to use it. There have been some efforts to remove underused items from the top-level name space (#25383), and I think there should be a good reason for adding a new one.

tl;dr: why not instead implement 4412.digit_sum(b=10)?

+1

@videlec
Copy link
Contributor

videlec commented Feb 16, 2020

comment:9

Unless the digit_sum provides a much faster way to perform the computation than sum(n.digits(b=10)), I don't see why we should add such method to integers. I don't think a method is more convenient. sum and prod are very much standard. If some people think they are not, then one can add an extra example to the documentation of Integer.digits.

Users would better learn a bit of Python rather being lost in an endless Sage namespace (that applies to the main namespace but also to objects).

@kedlaya
Copy link
Contributor

kedlaya commented Feb 16, 2020

comment:10

As I commented earlier, if n.digits had been written in the first place to return an iterable rather than a list, then there would be no speed differential.

But maybe a better solution would to be modify n.digits to give it an optional argument to return that iterable. Then sum(n.digits(b=10,iterable=True)) would be as fast as anything else.

And honestly, I think the use case for which this makes a difference is quite narrow.

@mkoeppe
Copy link
Contributor

mkoeppe commented Apr 14, 2020

comment:11

Batch modifying tickets that will likely not be ready for 9.1, based on a review of the ticket title, branch/review status, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.1, sage-9.2 Apr 14, 2020
@mkoeppe mkoeppe modified the milestones: sage-9.2, sage-9.3 Sep 5, 2020
@mkoeppe
Copy link
Contributor

mkoeppe commented Mar 15, 2021

comment:13

Setting new milestone based on a cursory review of ticket status, priority, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Mar 15, 2021
@mkoeppe
Copy link
Contributor

mkoeppe commented Jul 19, 2021

comment:14

Setting a new milestone for this ticket based on a cursory review.

@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Jul 19, 2021
@mkoeppe
Copy link
Contributor

mkoeppe commented Dec 18, 2021

comment:15

Stalled in needs_review or needs_info; likely won't make it into Sage 9.5.

@mkoeppe mkoeppe modified the milestones: sage-9.5, sage-9.6 Dec 18, 2021
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 Apr 2, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Sep 19, 2022
@mkoeppe mkoeppe removed this from the sage-9.8 milestone Jan 29, 2023
@Noel-Roemmele Noel-Roemmele self-assigned this Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants