# 3n+1

## 227 days ago by kefanf

def f(n): if n<1 or (type(n)==Integer)==False: return("Please input a positive integer") elif is_even(n): return(Integer(n/2)) else: return(3*n+1)
def g(n): if n<1 or (type(n)==Integer)==False: return("Please input a positive integer") elif is_even(n): return(Integer(n/2)) else: return(Integer((3*n+1)/2))
def peakf(n): fpeak = n while n > 1: n = f(n) if n > fpeak: fpeak = n return fpeak
def peakg(n): gpeak = n while n > 1: n = g(n) if n > gpeak: gpeak = n return gpeak
peakg(21323)
 47978 47978
peakf(1)
 1 1
peakf_list = [[1,1]] for i in srange(2,100000): fpeak = peakf(i) peakf_list.append([i,fpeak]) peakfovern_list = [[1,1]] for i in srange(2,100000): fpeak = peakf(i) fpeakovern = fpeak/i peakfovern_list.append([i,fpeak/i])
list_plot(peakf_list) list_plot(peakfovern_list) def ispeak(n): if peakf(n) == n: return True else: return False
ispeak(52) ispeak(8)
 True True
num_of_peak = 0 N = 100000 for i in srange(1,N): if ispeak(i): num_of_peak += 1 print(num_of_peak/N)
 1763/12500 1763/12500

 Traceback (click to the left of this block for traceback) ... __SAGE__ ^CTraceback (most recent call last): File "", line 1, in File "_sage_input_57.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("bnVtX29mX3BlYWsgPSAwCk4gPSAxMDAwMDAwCmZvciBpIGluIHNyYW5nZSgxLE4pOgogICAgaWYgaXNwZWFrKGkpOgogICAgICAgIG51bV9vZl9wZWFrICs9IDEKcHJpbnQobnVtX29mX3BlYWsvTik="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in File "/tmp/tmpoEZ0dC/___code___.py", line 6, in if ispeak(i): File "/tmp/tmp1er5pZ/___code___.py", line 3, in ispeak if peakf(n) == n: File "/tmp/tmpCUotWj/___code___.py", line 6, in peakf n = f(n) File "/tmp/tmps7cKSQ/___code___.py", line 7, in f return(Integer(n/_sage_const_2 )) File "sage/ext/interrupt/interrupt.pyx", line 203, in sage.ext.interrupt.interrupt.sage_python_check_interrupt (/usr/local/sage-6.10/src/build/cythonized/sage/ext/interrupt/interrupt.c:1891) File "sage/ext/interrupt/interrupt.pyx", line 88, in sage.ext.interrupt.interrupt.sig_raise_exception (/usr/local/sage-6.10/src/build/cythonized/sage/ext/interrupt/interrupt.c:925) KeyboardInterrupt __SAGE__