Thursday 8 January 2015

Output for Declination angle problem


The program can be found here: Declination angle

Declination angle(δ) on 1 of January is  0.000000
Declination angle(δ) on 2 of January is 19.732495
Declination angle(δ) on 3 of January is 21.323025
Declination angle(δ) on 4 of January is  3.309264
Declination angle(δ) on 5 of January is -17.747019
Declination angle(δ) on 6 of January is -22.486774
Declination angle(δ) on 7 of January is -6.552293
Declination angle(δ) on 8 of January is 15.406336
Declination angle(δ) on 9 of January is 23.200451
Declination angle(δ) on 10 of January is  9.664178
Declination angle(δ) on 11 of January is -12.757295
Declination angle(δ) on 12 of January is -23.449770
Declination angle(δ) on 13 of January is -12.582635
Declination angle(δ) on 14 of January is  9.852917
Declination angle(δ) on 15 of January is 23.229742
Declination angle(δ) on 16 of January is 15.249250
Declination angle(δ) on 17 of January is -6.751333
Declination angle(δ) on 18 of January is -22.544771
Declination angle(δ) on 19 of January is -17.610651
Declination angle(δ) on 20 of January is  3.514621
Declination angle(δ) on 21 of January is 21.408566
Declination angle(δ) on 22 of January is 19.619575
Declination angle(δ) on 23 of January is -0.207563
Declination angle(δ) on 24 of January is -19.843868
Declination angle(δ) on 25 of January is -21.235813
Declination angle(δ) on 26 of January is -3.103649
Declination angle(δ) on 27 of January is 17.881996
Declination angle(δ) on 28 of January is 22.427016
Declination angle(δ) on 29 of January is  6.352741
Declination angle(δ) on 30 of January is -15.562215
Declination angle(δ) on 31 of January is -23.169342
Declination angle(δ) on 1 of February is -9.474683
Declination angle(δ) on 2 of February is 12.930956
Declination angle(δ) on 3 of February is 23.447933
Declination angle(δ) on 4 of February is 12.406989
Declination angle(δ) on 5 of February is -10.040884
Declination angle(δ) on 6 of February is -23.257214
Declination angle(δ) on 7 of February is -15.090969
Declination angle(δ) on 8 of February is  6.949843
Declination angle(δ) on 9 of February is 22.601002
Declination angle(δ) on 10 of February is 17.472904
Declination angle(δ) on 11 of February is -3.719702
Declination angle(δ) on 12 of February is -21.492430
Declination angle(δ) on 13 of February is -19.505118
Declination angle(δ) on 14 of February is  0.415110
Declination angle(δ) on 15 of February is 19.953688
Declination angle(δ) on 16 of February is 21.146937
Declination angle(δ) on 17 of February is  2.897790
Declination angle(δ) on 18 of February is -18.015572
Declination angle(δ) on 19 of February is -22.365500
Declination angle(δ) on 20 of February is -6.152690
Declination angle(δ) on 21 of February is 15.716874
Declination angle(δ) on 22 of February is 23.136417
Declination angle(δ) on 23 of February is  9.284445
Declination angle(δ) on 24 of February is -13.103603
Declination angle(δ) on 25 of February is -23.444259
Declination angle(δ) on 26 of February is -12.230371
Declination angle(δ) on 27 of February is 10.228064
Declination angle(δ) on 28 of February is 23.282864
Declination angle(δ) on 1 of March is 14.931506
Declination angle(δ) on 2 of March is -7.147809
Declination angle(δ) on 3 of March is -22.655462
Declination angle(δ) on 4 of March is -17.333787
Declination angle(δ) on 5 of March is  3.924491
Declination angle(δ) on 6 of March is 21.574611
Declination angle(δ) on 7 of March is 19.389133
Declination angle(δ) on 8 of March is -0.622625
Declination angle(δ) on 9 of March is -20.061944
Declination angle(δ) on 10 of March is -21.056404
Declination angle(δ) on 11 of March is -2.691704
Declination angle(δ) on 12 of March is 18.147736
Declination angle(δ) on 13 of March is 22.302232
Declination angle(δ) on 14 of March is  5.952158
Declination angle(δ) on 15 of March is  5.952158
Declination angle(δ) on 16 of March is -15.870302
Declination angle(δ) on 17 of March is -23.101680
Declination angle(δ) on 18 of March is -9.093479
Declination angle(δ) on 19 of March is 13.275224
Declination angle(δ) on 20 of March is 23.438748
Declination angle(δ) on 21 of March is 12.052795
Declination angle(δ) on 22 of March is -10.414442
Declination angle(δ) on 23 of March is -23.306689
Declination angle(δ) on 24 of March is -14.770873
Declination angle(δ) on 25 of March is  7.345215
Declination angle(δ) on 26 of March is 22.708147
Declination angle(δ) on 27 of March is 17.193313
Declination angle(δ) on 28 of March is -4.128973
Declination angle(δ) on 29 of March is -21.655101
Declination angle(δ) on 30 of March is -19.271628
Declination angle(δ) on 31 of March is  0.830090
Declination angle(δ) on 1 of April is 20.168628
Declination angle(δ) on 2 of April is 20.964222
Declination angle(δ) on 3 of April is  2.485407
Declination angle(δ) on 4 of April is -18.278479
Declination angle(δ) on 5 of April is -22.237216
Declination angle(δ) on 6 of April is -5.751159
Declination angle(δ) on 7 of April is 16.022487
Declination angle(δ) on 8 of April is 23.065133
Declination angle(δ) on 9 of April is  8.901801
Declination angle(δ) on 10 of April is -13.445805
Declination angle(δ) on 11 of April is -23.431400
Declination angle(δ) on 12 of April is -11.874274
Declination angle(δ) on 13 of April is 10.600005
Declination angle(δ) on 14 of April is 23.328688
Declination angle(δ) on 15 of April is 14.609083
Declination angle(δ) on 16 of April is -7.542045
Declination angle(δ) on 17 of April is -22.759052
Declination angle(δ) on 18 of April is -17.051492
Declination angle(δ) on 19 of April is  4.333132
Declination angle(δ) on 20 of April is 21.733894
Declination angle(δ) on 21 of April is 19.152614
Declination angle(δ) on 22 of April is -1.037491
Declination angle(δ) on 23 of April is -20.273731
Declination angle(δ) on 24 of April is -20.870397
Declination angle(δ) on 25 of April is -2.278916
Declination angle(δ) on 26 of April is 18.407790
Declination angle(δ) on 27 of April is 22.170459
Declination angle(δ) on 28 of April is  5.549710
Declination angle(δ) on 29 of April is -16.173417
Declination angle(δ) on 30 of April is -23.026778
Declination angle(δ) on 1 of May is -8.709426
Declination angle(δ) on 2 of May is 13.615332
Declination angle(δ) on 3 of May is 23.422217
Declination angle(δ) on 4 of May is 11.694823
Declination angle(δ) on 5 of May is -10.784737
Declination angle(δ) on 6 of May is -23.348860
Declination angle(δ) on 7 of May is -14.446149
Declination angle(δ) on 8 of May is  7.738285
Declination angle(δ) on 9 of May is 22.808175
Declination angle(δ) on 10 of May is 16.908334
Declination angle(δ) on 11 of May is -4.536951
Declination angle(δ) on 12 of May is -21.810985
Declination angle(δ) on 13 of May is -19.032099
Declination angle(δ) on 14 of May is  1.244810
Declination angle(δ) on 15 of May is 20.377247
Declination angle(δ) on 16 of May is 20.774937
Declination angle(δ) on 17 of May is  2.072246
Declination angle(δ) on 18 of May is -18.535659
Declination angle(δ) on 19 of May is -22.101964
Declination angle(δ) on 20 of May is -5.347825
Declination angle(δ) on 21 of May is 16.323079
Declination angle(δ) on 22 of May is 22.986620
Declination angle(δ) on 23 of May is  8.516369
Declination angle(δ) on 24 of May is -13.783793
Declination angle(δ) on 25 of May is -23.411199
Declination angle(δ) on 26 of May is -11.514456
Declination angle(δ) on 27 of May is -11.514456
Declination angle(δ) on 28 of May is 10.968624
Declination angle(δ) on 29 of May is 23.367202
Declination angle(δ) on 30 of May is 14.282082
Declination angle(δ) on 31 of May is -7.933918
Declination angle(δ) on 1 of June is -22.855511
Declination angle(δ) on 2 of June is -16.763852
Declination angle(δ) on 3 of June is  4.740415
Declination angle(δ) on 4 of June is 21.886366
Declination angle(δ) on 5 of June is 18.910094
Declination angle(δ) on 6 of June is -1.452032
Declination angle(δ) on 7 of June is -20.479166
Declination angle(δ) on 8 of June is -20.677849
Declination angle(δ) on 9 of June is -1.865413
Declination angle(δ) on 10 of June is 18.662075
Declination angle(δ) on 11 of June is 22.031738
Declination angle(δ) on 12 of June is  5.145522
Declination angle(δ) on 13 of June is -16.471463
Declination angle(δ) on 14 of June is -22.944661
Declination angle(δ) on 15 of June is -8.322644
Declination angle(δ) on 16 of June is 13.951174
Declination angle(δ) on 17 of June is 23.398346
Declination angle(δ) on 18 of June is 11.333187
Declination angle(δ) on 19 of June is -11.151652
Declination angle(δ) on 20 of June is -23.383714
Declination angle(δ) on 21 of June is -14.116897
Declination angle(δ) on 22 of June is  8.128930
Declination angle(δ) on 23 of June is 22.901056
Declination angle(δ) on 24 of June is 16.618057
Declination angle(δ) on 25 of June is -4.943507
Declination angle(δ) on 26 of June is -21.960033
Declination angle(δ) on 27 of June is -18.786606
Declination angle(δ) on 28 of June is  1.659140
Declination angle(δ) on 29 of June is 20.579481
Declination angle(δ) on 30 of June is 20.579142
Declination angle(δ) on 1 of July is  1.658435
Declination angle(δ) on 2 of July is -18.787029
Declination angle(δ) on 3 of July is -21.959785
Declination angle(δ) on 4 of July is -4.942816
Declination angle(δ) on 5 of July is 16.618556
Declination angle(δ) on 6 of July is 22.900904
Declination angle(δ) on 7 of July is  8.128267
Declination angle(δ) on 8 of July is -14.117461
Declination angle(δ) on 9 of July is -23.383660
Declination angle(δ) on 10 of July is -11.151030
Declination angle(δ) on 11 of July is 11.333806
Declination angle(δ) on 12 of July is 23.398393
Declination angle(δ) on 13 of July is 13.950605
Declination angle(δ) on 14 of July is -8.323304
Declination angle(δ) on 15 of July is -22.944807
Declination angle(δ) on 16 of July is -16.470959
Declination angle(δ) on 17 of July is  5.146212
Declination angle(δ) on 18 of July is 22.031980
Declination angle(δ) on 19 of July is 18.661647
Declination angle(δ) on 20 of July is -1.866118
Declination angle(δ) on 21 of July is -20.678183
Declination angle(δ) on 22 of July is -20.478822
Declination angle(δ) on 23 of July is -1.451326
Declination angle(δ) on 24 of July is 18.910512
Declination angle(δ) on 25 of July is 21.886112
Declination angle(δ) on 26 of July is  4.739722
Declination angle(δ) on 27 of July is -16.764347
Declination angle(δ) on 28 of July is -22.855353
Declination angle(δ) on 29 of July is -7.933253
Declination angle(δ) on 30 of July is 14.282643
Declination angle(δ) on 31 of July is 23.367143
Declination angle(δ) on 1 of August is 10.967999
Declination angle(δ) on 2 of August is -11.515072
Declination angle(δ) on 3 of August is -23.411239
Declination angle(δ) on 4 of August is -13.783221
Declination angle(δ) on 5 of August is  8.517027
Declination angle(δ) on 6 of August is 22.986760
Declination angle(δ) on 7 of August is 16.322572
Declination angle(δ) on 8 of August is 16.322572
Declination angle(δ) on 9 of August is -5.348514
Declination angle(δ) on 10 of August is -22.102200
Declination angle(δ) on 11 of August is -18.535226
Declination angle(δ) on 12 of August is  2.072950
Declination angle(δ) on 13 of August is 20.775265
Declination angle(δ) on 14 of August is 20.376897
Declination angle(δ) on 15 of August is  1.244104
Declination angle(δ) on 16 of August is -19.032512
Declination angle(δ) on 17 of August is -21.810725
Declination angle(δ) on 18 of August is -4.536258
Declination angle(δ) on 19 of August is 16.908824
Declination angle(δ) on 20 of August is 22.808011
Declination angle(δ) on 21 of August is  7.737618
Declination angle(δ) on 22 of August is -14.446706
Declination angle(δ) on 23 of August is -23.348794
Declination angle(δ) on 24 of August is -10.784109
Declination angle(δ) on 25 of August is 11.695436
Declination angle(δ) on 26 of August is 23.422251
Declination angle(δ) on 27 of August is 13.614757
Declination angle(δ) on 28 of August is -8.710082
Declination angle(δ) on 29 of August is -23.026912
Declination angle(δ) on 30 of August is -16.172905
Declination angle(δ) on 31 of August is  5.550396
Declination angle(δ) on 1 of September is 22.170689
Declination angle(δ) on 2 of September is 18.407352
Declination angle(δ) on 3 of September is -2.279619
Declination angle(δ) on 4 of September is -20.870719
Declination angle(δ) on 5 of September is -20.273376
Declination angle(δ) on 6 of September is -1.036785
Declination angle(δ) on 7 of September is 19.153022
Declination angle(δ) on 8 of September is 21.733629
Declination angle(δ) on 9 of September is  4.332437
Declination angle(δ) on 10 of September is -17.051977
Declination angle(δ) on 11 of September is -22.758882
Declination angle(δ) on 12 of September is -7.541376
Declination angle(δ) on 13 of September is 14.609636
Declination angle(δ) on 14 of September is 23.328616
Declination angle(δ) on 15 of September is 10.599374
Declination angle(δ) on 16 of September is -11.874884
Declination angle(δ) on 17 of September is -23.431428
Declination angle(δ) on 18 of September is -13.445226
Declination angle(δ) on 19 of September is  8.902455
Declination angle(δ) on 20 of September is 23.065260
Declination angle(δ) on 21 of September is 16.021971
Declination angle(δ) on 22 of September is -5.751844
Declination angle(δ) on 23 of September is -22.237441
Declination angle(δ) on 24 of September is -18.278036
Declination angle(δ) on 25 of September is  2.486110
Declination angle(δ) on 26 of September is 20.964538
Declination angle(δ) on 27 of September is 20.168267
Declination angle(δ) on 28 of September is  0.829384
Declination angle(δ) on 29 of September is -19.272031
Declination angle(δ) on 30 of September is -21.654829
Declination angle(δ) on 1 of October is -4.128277
Declination angle(δ) on 2 of October is 17.193794
Declination angle(δ) on 3 of October is 22.707970
Declination angle(δ) on 4 of October is  7.344544
Declination angle(δ) on 5 of October is -14.771422
Declination angle(δ) on 6 of October is -23.306611
Declination angle(δ) on 7 of October is -10.413809
Declination angle(δ) on 8 of October is 12.053401
Declination angle(δ) on 9 of October is 23.438770
Declination angle(δ) on 10 of October is 13.274641
Declination angle(δ) on 11 of October is -9.094131
Declination angle(δ) on 12 of October is -23.101801
Declination angle(δ) on 13 of October is -15.869782
Declination angle(δ) on 14 of October is  5.952842
Declination angle(δ) on 15 of October is 22.302450
Declination angle(δ) on 16 of October is 18.147289
Declination angle(δ) on 17 of October is -2.692406
Declination angle(δ) on 18 of October is -21.056715
Declination angle(δ) on 19 of October is -20.061577
Declination angle(δ) on 20 of October is -20.061577
Declination angle(δ) on 21 of October is -0.621918
Declination angle(δ) on 22 of October is 19.389530
Declination angle(δ) on 23 of October is 21.574334
Declination angle(δ) on 24 of October is  3.923794
Declination angle(δ) on 25 of October is -17.334263
Declination angle(δ) on 26 of October is -22.655279
Declination angle(δ) on 27 of October is -7.147136
Declination angle(δ) on 28 of October is 14.932051
Declination angle(δ) on 29 of October is 23.282779
Declination angle(δ) on 30 of October is 10.227427
Declination angle(δ) on 31 of October is -12.230974
Declination angle(δ) on 1 of November is -23.444274
Declination angle(δ) on 2 of November is -13.103017
Declination angle(δ) on 3 of November is  9.285094
Declination angle(δ) on 4 of November is 23.136532
Declination angle(δ) on 5 of November is 15.716350
Declination angle(δ) on 6 of November is -6.153372
Declination angle(δ) on 7 of November is -22.365712
Declination angle(δ) on 8 of November is -18.015119
Declination angle(δ) on 9 of November is  2.898491
Declination angle(δ) on 10 of November is 21.147242
Declination angle(δ) on 11 of November is 19.953316
Declination angle(δ) on 12 of November is  0.414403
Declination angle(δ) on 13 of November is -19.505510
Declination angle(δ) on 14 of November is -21.492148
Declination angle(δ) on 15 of November is -3.719004
Declination angle(δ) on 16 of November is 17.473375
Declination angle(δ) on 17 of November is 22.600813
Declination angle(δ) on 18 of November is  6.949168
Declination angle(δ) on 19 of November is -15.091510
Declination angle(δ) on 20 of November is -23.257124
Declination angle(δ) on 21 of November is -10.040245
Declination angle(δ) on 22 of November is 12.407589
Declination angle(δ) on 23 of November is 23.447942
Declination angle(δ) on 24 of November is 12.930366
Declination angle(δ) on 25 of November is -9.475329
Declination angle(δ) on 26 of November is -23.169451
Declination angle(δ) on 27 of November is -15.561686
Declination angle(δ) on 28 of November is  6.353421
Declination angle(δ) on 29 of November is 22.427222
Declination angle(δ) on 30 of November is 17.881538
Declination angle(δ) on 1 of December is -3.104349
Declination angle(δ) on 2 of December is -21.236112
Declination angle(δ) on 3 of December is -19.843492
Declination angle(δ) on 4 of December is -0.206856
Declination angle(δ) on 5 of December is 19.619962
Declination angle(δ) on 6 of December is 21.408278
Declination angle(δ) on 7 of December is  3.513922
Declination angle(δ) on 8 of December is -17.611118
Declination angle(δ) on 9 of December is -22.544577
Declination angle(δ) on 10 of December is -6.750656
Declination angle(δ) on 11 of December is 15.249787
Declination angle(δ) on 12 of December is 23.229646
Declination angle(δ) on 13 of December is  9.852276
Declination angle(δ) on 14 of December is -12.583231
Declination angle(δ) on 15 of December is -23.449773
Declination angle(δ) on 16 of December is -12.756702
Declination angle(δ) on 17 of December is  9.664823
Declination angle(δ) on 18 of December is 23.200554
Declination angle(δ) on 19 of December is 15.405803
Declination angle(δ) on 20 of December is -6.552972
Declination angle(δ) on 21 of December is -22.486975
Declination angle(δ) on 22 of December is -17.746556
Declination angle(δ) on 23 of December is  3.309964
Declination angle(δ) on 24 of December is 21.323319
Declination angle(δ) on 25 of December is 19.732113
Declination angle(δ) on 26 of December is -0.000707
Declination angle(δ) on 27 of December is -19.732877
Declination angle(δ) on 28 of December is -21.322730
Declination angle(δ) on 29 of December is -3.308564
Declination angle(δ) on 30 of December is 17.747481
Declination angle(δ) on 31 of December is 22.486574

Declination angle for all 365 days

Question:To find the declination angle using python. The formula for the declination angle is given by:
δ = 23.45*sin[360*n/365]
δ(Declination angle) is the angle made by the line joining the centers of the sun and the earth with the projection of this line on equatorial plane(equator of earth). To make it simple a simple diagram can be given as follows:

One thing to be remembered about the earth is that it is not vertical when compared to sun. So there will always be an angle with the sun. If you don't understand anything just ignore the theory and generate the result using the formula. You can find more here.
Here the question is: considering n = 1 as January 1st and December 31 as n = 365 and ignoring the leap years generate the values of Declination angle using python.

Answer 

# -*- coding: utf-8 -*-
#Firstly indicated that we are going to use UTF-8 format
import math #Imported the math module
def declination_angle(n): #Defined a function for declination angle
    angle = 23.45*math.sin(360*n/365) #Defined the formula
    return angle

def month(n):#Defined a function to seperate the month on the basis of day number
    if n>=1 and n<=31:
        return "January"#31 days
    elif n>31 and n<=59:
        return "February"#28 days
    elif n>59 and n<=90:
        return "March"#31 days
    elif n>90 and n<=120:
        return "April"#30 days
    elif n>120 and n<=151:
        return "May"#31 days
    elif n>151 and n<=181:
        return "June"#30 days
    elif n>181 and n<=212:
        return "July"#31 days
    elif n>212 and n<=243:
        return "August"#31 days
    elif n>243 and n<=273:
        return "September"#30 days
    elif n>273 and n<=304:
        return "October"#31 days
    elif n>304 and n<=334:
        return "November"#30 days
    elif n>334 and n<=365:
        return "December"#31 days
    
def day(n):#Function to make the day number to month number
    #For example n = 53 will become 22(53-31)
    if n>=1 and n<=31:#Number of days before the month are indicated
        return n#0 days before January
    elif n>31 and n<=59:
        return n-31#31 days before February
    elif n>59 and n<=90:
        return n-59#59 days days before March
    elif n>90 and n<=120:
        return n-90#90 days before April
    elif n>120 and n<=151:
        return n-120#120 days before May
    elif n>151 and n<=181:
        return n-151#151 days before June
    elif n>181 and n<=212:
        return n-181#181 days before July
    elif n>212 and n<=243:
        return n-212#212 days before August
    elif n>243 and n<=273:
        return n-243#243 days before September
    elif n>273 and n<=304:
        return n-273#273 days before October
    elif n>304 and n<=334:
        return n-304#304 days before November
    elif n>334 and n<=365:
        return n-334#334 days before December

for i in range(1,366): #Looping for 365 days
    angle = declination_angle(i) #Storing the angle in a variable
    mon = month(i) #storing the month in mon variable
    print u"Declination angle(δ)", #indicated "u" before string to say that there is "δ" in string
    print "on", #comma at the end of string to make it come on same day
    print day(i),
    print "of",
    print mon,
    print "is",
    print "{}".format(angle)#Format method to indicate angle

Output:

Declination angle(δ) on 1 of January is  0.000000
Declination angle(δ) on 2 of January is 19.732495
Declination angle(δ) on 3 of January is 21.323025
...........


Full output can be seen here:Output for declination angle problem
I have written the code and commented the code so that everyone will be able to understand the program easily. Each part of the program is as folllows:


# -*- coding: utf-8 -*-
This will tell the interpreter that the code is in UTF-8 format. And if we use other characters like delta, alpha then interpreter will show the symbols and otherwise it will not.



import math #Imported the math module

The above statement will import the Math module from where we can use the sine function. We can also use the "from" method where we will not use the "math.sin" in the code. But the above way is recommended so that there will not be any confusion



def declination_angle(n): #Defined a function for declination angle
    angle = 23.45*math.sin(360*n/365) #Defined the formula
    return angle

This will define a function named declination_angle which will take "n" as argument. This function has a variable name angle which contains the formula. This function will take the value of the "n" and return the declination angle which is stored in the angle variable



def month(n):#Defined a function to seperate the month on the basis of day number
    if n>=1 and n<=31:
        return "January"#31 days
    elif n>31 and n<=59:
        return "February"#28 days
    elif n>59 and n<=90:
        return "March"#31 days
    elif n>90 and n<=120:
        return "April"#30 days
    elif n>120 and n<=151:
        return "May"#31 days
    elif n>151 and n<=181:
        return "June"#30 days
    elif n>181 and n<=212:
        return "July"#31 days
    elif n>212 and n<=243:
        return "August"#31 days
    elif n>243 and n<=273:
        return "September"#30 days
    elif n>273 and n<=304:
        return "October"#31 days
    elif n>304 and n<=334:
        return "November"#30 days
    elif n>334 and n<=365:
        return "December"#31 days

This will define a function which will take the day number in the year as argument. We have used series of if elif which will divide the day numbers and will return the month. This is useful to give an output with the month name so that the user will understand the output easily.



def day(n):#Function to make the day number to month number
    #For example n = 53 will become 22(53-31)
    if n>=1 and n<=31:#Number of days before the month are indicated
        return n#0 days before January
    elif n>31 and n<=59:
        return n-31#31 days before February
    elif n>59 and n<=90:
        return n-59#59 days days before March
    elif n>90 and n<=120:
        return n-90#90 days before April
    elif n>120 and n<=151:
        return n-120#120 days before May
    elif n>151 and n<=181:
        return n-151#151 days before June
    elif n>181 and n<=212:
        return n-181#181 days before July
    elif n>212 and n<=243:
        return n-212#212 days before August
    elif n>243 and n<=273:
        return n-243#243 days before September
    elif n>273 and n<=304:
        return n-273#273 days before October
    elif n>304 and n<=334:
        return n-304#304 days before November
    elif n>334 and n<=365:
        return n-334#334 days before December

We know that every month has days 31 or 30 or 28 and the dates of the month are represented between these numbers depending on the month. But we use an input of numbers from 1 to 365 which will represent the day number in the year instead of month. The above function "day" is uses a series of if and elif to represent the day in the month instead of the year.



for i in range(1,366): #Looping for 365 days
    angle = declination_angle(i) #Storing the angle in a variable
    mon = month(i) #storing the month in mon variable
    print u"Declination angle(δ)", #indicated "u" before string to say that there is "δ" in string
    print "on", #comma at the end of string to make it come on same day
    print day(i),
    print "of",
    print mon,
    print "is",
    print "{}".format(angle)#Format method to indicate angle

Finally we will loop to generate the result of 365 days. For this we have used for loop. This loop will give our functions(declination_angle, month) each day in the year. Here we have used a series of print statements to show our output for our users.
It should be noted that we have used "u" in front of the string, this will tell the interpreter to print the characted δ in the same way. For more information you can see here: Unicode HOWTO &mdash; Python 2.7.9 documentation

Please do contact me if you have a better solution than this. Thanks! 

Monday 5 January 2015

Finding factor


Question:
a) Find the factors of a number using "for" loop. Find the factors of 200.
b) Find the factors of a number using "while" loop. Find the factors of 200.
c) Write a function using any of the two loops. Find the factors of 200.

Answer: I have commented the code so that everyone will be able to understand the code. Please do feel to contact me if you have a doubt or if you have found a mistake in the program. Please do contact me if you think that you have a good program than this, so that it can be shared here.
a) 

n = 200#Initialize number 200 as variable
for i in range(2,n):#Looping through the number 
 if n%i == 0:#Check whether a number is dividing or not
  print "Factor has been found",i #Print if the factor is found

Output:

Factor has been found 2
Factor has been found 4
Factor has been found 5
Factor has been found 8
Factor has been found 10
Factor has been found 20
Factor has been found 25
Factor has been found 40
Factor has been found 50
Factor has been found 100

b) 

n = 200#Initialize number 200 as variable
i = 2 #For while loop initialization
while i<n:#Looping through the numbers greater than 2 and less than n
        if n%i == 0:#Check whether a number is dividing or not
                print "Factor has been found",i #Print if the factor is found
        i += 1
Output:

Factor has been found 2
Factor has been found 4
Factor has been found 5
Factor has been found 8
Factor has been found 10
Factor has been found 20
Factor has been found 25
Factor has been found 40
Factor has been found 50
Factor has been found 100
c)  I will use for loop for my function. You can use anything of your choice. If you use while loop you will have to replace the for loop code with the while loop code as it is. As simple as that. :)


def factors(n):#Function definition
        i = 2 #For while loop initialization
        while i<n:#Looping through the numbers greater than 2 and less than n
                if n%i == 0:#Check whether a number is dividing or not
                        print "Factor has been found",i #Print if the factor is found
                i += 1

factors(200)#Function Call
Output:


Factor has been found 2
Factor has been found 4
Factor has been found 5
Factor has been found 8
Factor has been found 10
Factor has been found 20
Factor has been found 25
Factor has been found 40
Factor has been found 50
Factor has been found 100

Find the code and compile it directly without the python interpreter on your personal computer, here: mycode
Note: This is my own code and is not copied.

Prime number check in python


Question:
a) Write a simple program to find whether the number is prime or not. Check whether 10 is prime or not.
b) Write a function in python to find the whether a number is prime not. Return True if prime and False if not. Check whether 10 is prime or not.
c) Write a program in python which takes a number input from the user and returns if the number is prime or not. Check whether 10 is prime or not.

Answer: I have commented the code please do ask if you have any doubt. If you feel that you have a better solution than this then please do send it to me.
a) 
n = 10 #variable declaration
if n>1:#tO check if the number is greater than 1
 for i in range(2,n):
 #Loop less than the number
 #Loop excluding the number and greater than 1
  if n%i == 0:#Loop to check the divisibility by any numbers
   print "The number is not a prime number" #Printing the output
   break
 
  print "The number is a prime number" #Printing the output 

Output:

The number is not a prime number

b)

def is_prime(n):#Function declaration
 if n>1:#tO check if the number is greater than 1
  for i in range(2,n):
  #Loop less than the number
  #Loop excluding the number and greater than 1
   if n%i == 0:#Loop to check the divisibility by any numbers
    return False #To return False
  
   return True #To return True 

is_prime(10)

Output:

False

c)

def is_prime(n):#Function declaration
 if n>1:#tO check if the number is greater than 1
  for i in range(2,n):
  #Loop less than the number
  #Loop excluding the number and greater than 1
   if n%i == 0:#Loop to check the divisibility by any numbers
    return False #To return False
  
   return True #To return True 

x = int(raw_input("Enter the number to check"))#INterger input from the user
is_prime(x)

Output:

>>> Enter the number to check
...10
>>>False

Copyright: I haven't copied above from anywhere. This is my original work.

Thursday 1 January 2015

Fibonacci Sequence

Question: Fibonacci sequence starts from 0,1 and their sum is 1 and the sum of 1,1 is 2 and this continues. See the picture and you will understand:

In the similar way using python generate first 15 Fibonacci numbers.

Answer: I have used functional approach. If you don't know what functions are, then remove the first line with def fibanocci(n): and then instead of n add the number of Fibonacci numbers you want. This will give you the result. But I suggest you to use functions as you write the code once and generate the output the required times. You can even test your answer with the question example(if given). I wrote the code such that it will be easy to understand, but if you don't understand please do ask me I will explain. Also post your Fibonacci code.


#Function to generate n fibanocci numbers
def fibanocci(n):
        a = 0
        b = 1
        numbers = []
        numbers.append(a)
        while len(numbers) < n+2:
                numbers.append(b)
                temp = a
                a = b
                b = a+temp
        return numbers
print fibonacci(15)
Output:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]

Sum of even Fibonacci numbers below 4 million


Question: We know that fibonacci sequence is 0,1,1,2,3,5,8,13........ This is obtained by taking the first two whole numbers 0, 1 and then we add them we get 1, so the sequence becomes 0,1,1 now consider the two numbers 1,1 and this gives 2 and in this way the sequence is obtained. For further more information you can find about fibonacci sequence here: Fibonacci Sequence.

Let us assume the first 10 Fibonacci numbers(Neglect 0 and 1): 1,2,3,5,8,13,21,34,55,89. Now the even Fibonacci numbers become: 2,8,34. And the sum is 44. In this way we have to find the sum of even fibonacci numbers where the largest number in the group(In the top ten Fibonacci numbers is 34) is less than 4 million. 
Answer: I have commented the code and tried to make it as simple as possible. Please do contact me if you have a solution which is a way different from this so that it can be published here. Also please do comment if you have a doubt. Doubts are dangerous.


#Function to check if number is even.
#Returns True if even and else False
def is_even(n):
        if n%2 == 0:
                return True
        else:
                return False

#Function to generate n fibanocci numbers
def fibanocci(n):
        a = 0
        b = 1
        numbers = []
        numbers.append(a)
        while len(numbers) < n+2:
                numbers.append(b)
                temp = a
                a = b
                b = a+temp
        del numbers[0]
        del numbers[1]
        return numbers

#Function to generate even fibanocci numbers
def even_fibanocci(n):
        lis = fibanocci(n)
        even = []
        for i in lis:
                if is_even(i):
                        even.append(i)
        return even
#Iteration to generate sum of even numbers
j = 1
while True:
        lis = even_fibanocci(j)
        if len(lis) > 0:
                if lis[len(lis)-1] < 4000000:
                        lis = even_fibanocci(j)
                        largest = "Sum(even) of first",j,"fibonacci number is",sum(lis)
                else:
                        break
        j += 1
print largest
Output:

('Sum(even) of first', 34, 'fibonacci number is', 4613732)

So the answer for the question is 4613732. Note: This problem was taken from Project Euler. And the solution is my own.