Swift Algorithm notes-5

Source: Internet
Author: User

viewcontroller.swift//learn5////Created by MYHASPL on 16/1/23.//Copyright (c) 2016 MYHASPL.  All rights Reserved.//import Cocoaimport Foundationclass viewcontroller:nsviewcontroller {override func ViewDidLoad ()    {super.viewdidload ()//Do any additional setup after loading the view. } override Var Representedobject:anyobject?

{didset {//Update the view, if already loaded. }}private func Comresult (inputnum:double)->double{//5*x^7-3*x^5+16*x^2+7*x+90=0 let MYRESULT:DOUBL E = 5 * POW (inputnum,7)-3 * POW (inputnum,5) + * POW (inputnum,2) + 7 * inputnum + + return Myresult} @IBOut Let weak var result:nstextfield! @IBAction func Compute (sender:anyobject) {//5*x^7-3*x^5+16*x^2+7*x+90=0//to find the solution of the Yi Yuanfang, the maximum solution range [ -100000,100000] let TR Ycount = accuracy:double var = 0.00000000000001 var answer:double?

=nil//Expected solution range Var leftbound:double?=nil var rightbound:double?=nil for var bound:double=1;bound<100000 00;bound*=10{let Leftres=comresult (-bound) let Rightres=comresult (bound) if (Leftres*rightres) < 0 {Leftboun D = (-bound) Rightbound = Bound Break}} if (Leftbound==nil | | rightbound==nil) {return}//the solution for calculating equations for I in 1. . trycount{result.stringvalue=string (i) let center=leftbound!+ (rightbound!-leftbound!) /2let Leftres:double=comresult (leftbound!) Let Rightres:double=comresult (rightbound!) Let Centres:double=comresult (center) if centres==0 {answer=centerbreak}else if ABS (rightbound!-leftbound!) < Accuracy {answer=leftbound!break}else if Leftres*centres<0{rightbound=center}else if rightres*centres<0{ Leftbound=center}} If let ans=answer{//equation has solution result.stringvalue= "solution:" +string (Stringinterpolationsegment:ans) + "" Result.stringvalue + = "value of the solution into the equation:" +string (Stringinterpolationsegment:comresult (ANS))} }}

the whole content of this blog is original. If reproduced please indicate source http://blog.csdn.net/myhaspl/

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

Swift Algorithm notes-5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.