本文目錄一覽:
- 1、Python程序,定義一個 prime() 函數求整數 n 以內(不包括n)的所有素數(1不是素數)
- 2、用遞歸求素數分解 求高手修改 !!
- 3、python將一個正整數分解質因數.
- 4、Python 實現遞歸
- 5、使用python將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。應該要怎麼做?
Python程序,定義一個 prime() 函數求整數 n 以內(不包括n)的所有素數(1不是素數)
定義一個 prime() 函數求整數 n 以內(不包括n)的所有素數(1不是素數),br並返回一個按照升序排列的素數列表。使用遞歸來實現一個二分查找算法br函數bi_search(),該函數實現檢索任意一個整數在 prime() 函數生成的素數列br表中位置(索引)的功能,並返回該位置的索引值,若該數不存在則返回 -1。brbr輸入格式:br第一行為正整數 nbr接下來若干行為待查找的數字,每行輸入一個數字br輸出格式:br每行輸出相應的待查找數字的索引值br輸入樣例:br10br2br4br6br7br輸出樣例:br0br-1br-1br3br
用遞歸求素數分解 求高手修改 !!
#include stdio.h
#include math.h
/*聲明函數int isPrime(int n)判斷一個數是否為素數*/
int isPrime(int n);
/*聲明函數max(int num)求一個數的最大為素數的公約數*/
int max(int num);
/*聲明函數void recurPrintFactor(int n)打印素數分解的結果*/
void recurPrintFactor(int n);
main()
{
/*a起始數,b結尾數,n被操作數*/
int a,b,n;
printf(“Please input two integers:”);
scanf(“%d %d”,a,b); //最好以空格做分隔符
printf(“The result is:\n”);
/*從a到b逐一對數進行操作*/
for(n=a;n=b;n++)
{
printf(“%d=”,n);
recurPrintFactor(n);
putchar(‘\n’); //換行
}
printf(“\n”);
system(“pause”);
return 0;
}
int isPrime(int n)
{
int i=2,limit;
int isPrim=1;
limit=(int)(sqrt(n)+0.5);
while (i=limit isPrim==1){
if ((n%i)==0)
isPrim=0;
else
i++;
}
return isPrim;
}
int max(int n)
{
int i;
i=n-1;
while((n%i!=0)||(isPrime(i)==0))
{
i=i-1;
}
return i;
}
void recurPrintFactor(int n)
{
int num = max(n); //max只調用一次就行了,不要重複調用
if(num==1) //終止條件一定要放前面
{
printf(“%d”,n);
}
else
{
printf(“%d*”,num);
/*輸入的值=前一個因子最大素公約數*/
recurPrintFactor(n/num);
}
return;
}
python將一個正整數分解質因數.
n not in [1] 就是n不等於1
print ‘{}*’.format(index)是在最後將輸入的n打印成質因數,就是變成1*2*5這種樣式
Python 實現遞歸
一、使用遞歸的背景
先來看一個☝️接口結構:
這個孩子,他是一個列表,下面有6個元素
展開children下第一個元素[0]看看:
發現[0]除了包含一些字段信息,還包含了 children 這個字段(喜當爹),同時這個children下包含了2個元素:
展開他的第一個元素,不出所料,也含有children字段(人均有娃)
可以理解為children是個對象,他包含了一些屬性,特別的是其中有一個屬性與父級children是一模一樣的,他包含父級children所有的屬性。
比如每個children都包含了一個name字段,我們要拿到所有children里name字段的值,這時候就要用到遞歸啦~
二、find_children.py
拆分理解:
1.首先import requests庫,用它請求並獲取接口返回的數據
2.若children以上還有很多層級,可以縮小數據範圍,定位到children的上一層級
3.來看看定義的函數
我們的函數調用:find_children(node_f, ‘children’)
其中,node_f:json字段
children:遞歸對象
以下這段是實現遞歸的核心:
if items[‘children’]:
items[‘children’]不為None,表示該元素下的children字段還有子類數據值,此時滿足if條件,可理解為 if 1。
items[‘children’]為None,表示該元素下children值為None,沒有後續可遞歸值,此時不滿足if條件,可理解為 if 0,不會再執行if下的語句(不會再遞歸)。
至此,每一層級中children的name以及下一層級children的name就都取出來了
希望到這裡能幫助大家理解遞歸的思路,以後根據這個模板直接套用就行
(晚安啦~)
源碼參考:
使用python將一個正整數分解質因數。例如:輸入90,打印出90=2*3*3*5。應該要怎麼做?
對n進行分解質因數,應先找到一個最小的質數k,然後按下述步驟完成:
(1)如果這個質數恰等於n,則說明分解質因數的過程已經結束,打印出即可。
(2)如果nk,但n能被k整除,則應打印出k的值,並用n除以k的商,作為新的正整數你n,重複執行第一步。
(3)如果n不能被k整除,則用k+1作為k的值,重複執行第一步。
程序源代碼:
實例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def reduceNum(n):
print ‘{} = ‘.format(n),
if not isinstance(n, int) or n = 0 :
print ‘請輸入一個正確的數字 !’
exit(0)
elif n in [1] :
print ‘{}’.format(n)
while n not in [1] : # 循環保證遞歸
for index in xrange(2, n + 1) :
if n % index == 0:
n /= index # n 等於 n/index
if n == 1:
print index
else : # index 一定是素數
print ‘{} *’.format(index),
break
reduceNum(90)
reduceNum(100)
以上實例輸出結果為:
90 = 2 * 3 * 3 * 5100 = 2 * 2 * 5 * 5
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/237613.html