Factorial digit sum

0x1D

Kıdemli Üye
23 Nis 2020
2,650
78
MARS

n! means n × (n − 1) × ... × 3 × 2 × 1

For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!


carbon-17.png


Kod:
def factorial(num: int):
    fac = 1
    for _ in range(1, num+1):
        fac = fac*_

    return fac

def nsum(num):
    summ = []
    for _ in str(num):
        summ.append(int(_))

    return sum(summ)
        

print(nsum(factorial(100)))



https://projecteuler.net/problem=20
 
19 Eki 2020
149
1
Kod:
[font=monospace][color=#008000][b]def[/b][/color] [color=#0000FF]fact[/color](num: [color=#008000]int[/color]) [color=#666666]->[/color] [color=#008000]int[/color]:
    [color=#008000][b]if[/b][/color] num [color=#666666]==[/color] [color=#666666]1[/color]:
        [color=#008000][b]return[/b][/color] [color=#666666]1[/color]

    [color=#008000][b]return[/b][/color] num[color=#666666]*[/color]fact(num [color=#666666]-[/color] [color=#666666]1[/color])

[color=#008000][b]print[/b][/color]([color=#008000]sum[/color]([color=#008000]map[/color]([color=#008000]int[/color], [color=#008000]str[/color](fact([color=#666666]100[/color])))))
[/font]
 

zztri

Yaşayan Forum Efsanesi
9 Tem 2015
10,053
388
Ankara
Kod:
def factorial(num: int) -> int:
  if num<3:return num
  return num*factorial(num-1)
sum=0
number=factorial(100)
while number>0:
  sum+=number%10
  number//=10
print(sum)

Conversion from int to string is a TOUGH operation. Then changing char back to a number is, again, tough. Prefer the easiest path resource-wise; which's basic arithmetic operations in this case.


Bir sayıyı string'e çevirmek PAHALI bir işlemdir. Sonra her karakteri geri sayıya çevirmek yine pahalıdır. En ucuz işlemleri tercih edin; temel aritmetik operasyonları...
 
Moderatör tarafında düzenlendi:
19 Eki 2020
149
1
Kod:
def factorial(num: int) -> int:
  if num<3:return num
  return num*factorial(num-1)
sum=0
number=factorial(100)
while number>0:
  sum+=number%10
  number//=10
print(sum)

But if we enter only a large number, wouldn't it perform less than the other process?zztri By the way, it's forbidden to speak Turkish in this section.
 
Moderatör tarafında düzenlendi:

zztri

Yaşayan Forum Efsanesi
9 Tem 2015
10,053
388
Ankara
yalnız büyük bir sayı girersek, diğer işleme göre performanssız olmaz mı? zztri ayrıca burada türkçe konuşmak yasak.

Conversely, the larger the number, the more tough the string conversion becomes. Because it means translating the 8 bytes in memory step by step, assuming it is a long int, similar to what I did.
 
Moderatör tarafında düzenlendi:
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.