본문 바로가기

파이썬

(66)
[EP0062] 같은 숫자로 이루어진 세제곱이 다섯개 #!/usr/bin/env python # Project Euler 62 # http://projecteuler.net/index.php?section=problems&id=62 # Problem 62 # # The cube, 41063625 (3453), can be permuted to produce two other cubes: # 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest # cube which has exactly three permutations of its digits which are also # cube. # # Find the smallest cube for which exactly five permut..
[Python] 2117658/8642334 와 연분수표현 2117658/8642334 각 디지트의 연분수꼴의 값이 같다는 신기한 글을 봐서 파이썬으로 계산을 해 봤다. >>> from fractions import Fraction >>> l1 = list(map(int, "2117658")) >>> l2 = list(map(int, "8642334")) >>> def frac(l1, l2): if len(l1) == 1: return Fraction(l1[0], l2[0]) return l1[0]/(l2[0] + frac(l1[1:], l2[1:])) >>> frac(l1, l2) Fraction(37, 151) >>> Fraction(2117658, 8642334) Fraction(37, 151) 이런 꼴의 결과를 주는 다른 숫자도 찾아보고 싶네.
파이썬 자리수곱의합 문제 P(n)은 n을 십진수로 표시했을 때 0이 아닌 각 자리수의 곱으로 정의한다. 다음 값을 구하라. P(1) + P(2) + P(3) + ... + P(999999) 우선 무식한 방법 #!/usr/bin/python ########################################################## # ZiffernProdukte # --------------------------------------------------------- # # --------------------------------------------------------- # Mit P(n) bezeichnen wir das Produkt aller Ziffern # 1,2,3,4,5,6,7,8,9 in d..
파이썬 베쎌함수 그래프 그리기 from scipy.special import jv import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 30, 0.02) # https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.jv.html#scipy.special.jv ys = [ jv(i, x) for i in range(3) ] for y in ys: plt.plot(x, y) plt.grid() plt.show() 베셀함수 (Bessel Function) 은 파이썬의 scipy.special 모듈의 여러 특수함수들 중에 하나인 jv 함수로 제공된다. 위 코드를 실행하여 만들어진 그래프는 다음과 같다.
0과 1 사이에서 랜덤하게 뽑은 숫자를 평균적으로 몇 번 더해야 1보다 커질까요? # 0과 1 사이에서 랜덤하게 뽑은 숫자를 평균적으로 몇 번 더해야 1보다 커질까요? import numpy as np import math import random def test(): s = 0 cnt = 0 while s < 1: r = random.random() s += r cnt += 1 return cnt def n_tests(N): tests = [test() for _ in range(N)] # print(tests) m = np.mean(tests) s = np.std(tests) print(m, s, N) def main(): for n in [100, 10000, 1000000, 10000000]: for _ in range(3): n_tests(n) print(math.e) if ..
[파이썬초보] AttributeError: 'NoneType' object has no attribute 이런 에러에 대해 질문을 하는 걸 자주 봐서 포스팅을 하나 만들어 놓습니다. 에러메시지를 해석해 보면, "'NoneType' 객체는 ~~ 애트리뷰트가 없습니다."라는 뜻입니다. NoneType 객체는 사실 None 입니다. 파이썬의 None 은 자바나 C의 Null 같은 것입니다. 그래서 결국 None.someattr 이런식의 코드가 유효하지 않기 때문에 발생하는 에러입니다. 제가 만든 예제를 보고 이해해 보도록 합시다. >>> class dummy: def bark(self): print("dum dum") >>> def get_dummy(n): if n > 10: return dummy() return >>> d = get_dummy(21) >>> d.bark() dum dum >>> d2 = get..
[EP 047] 소인수분해의 소수의 갯수가 4개 문제는 이렇다. 644는 소인수분해하였을 때, 22 x 7 x 23 으로 세 개의 서로다른 소수로 이루어진다. n, n+1, n+2, n+3 이 모두 네 개의 서로다른 소수로 이루어지는 최소의 n을 구하라. 1부터 차례대로 소인수분해를 해 나가야 하기 때문에, 완전한 소수의 리스트를 관리할 수 있다. 소수판정이 매우 효율적이라서 기쁘다. 또한 소인수분해를 기록하는 리스트도 기록해 나가는데, 일단 하나의 소수만 발견하면 기록된 리스트를 이용해서 매우 효율적으로 원하는 값을 구할 수 있다. 소스를 봐라. # Author : DwYoon # PROJECT EULER # http://projecteuler.net/index.php?section=problems&id=47 # PROBLEM 47 import ti..
[PYTHON|SO번역] pip search 실행시에 XMLRPC API is currently disabled due to unmanageable load 에러 발생 https://stackoverflow.com/questions/66375972/getting-error-with-pip-search-and-pip-install Getting error with pip search and pip install hi it is about two days I am getting this error: ERROR: XMLRPC request failed [code:-32500] RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in ... stackoverflow.com 패키지를 검색하는 pip search 명령을 실행하면 에러가 발생하고 있다...