Question

add 1 + 2 + 3 + 4 + .... + 1,000,000 = ? then add...

add 1 + 2 + 3 + 4 + .... + 1,000,000 = ?

then add 1,000,000 + 999,999 + 999,998 + ... + 4 + 3 + 2 + 1 = ?

are they the same?

then repeat this with double floating point using 100 billion

Please Program this in PYTHON

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The explanation is done in the code itself. If you have any queries write a comment. If you have understood upvote. Thank you

SOLUTION:

##sum from 1 to 1000000
sum1=0
##sum from 1000000 to 1
sum2=0

##iterate from 1 to 1000000 and calculate sum
for i in range(1000000+1):
sum1=sum1+i
##print sum
print("Sum from 1 to 1000000 ",sum1)
##iterate from 1000000 to 1 and calculate sum
for i in range(1000000,0,-1):
sum2=sum2+i
##print sum
print("Sum from 1000000 to 1 ",sum2)

if(sum1==sum2):
print("Both are same ")
else:
print("Both are not same")
##variable to store
sumOfFloat=0.0
sumOfFloatreverse=0.0
number=1
##calculating the number by adding 0.1 every time till
while(number<=1000000):
sumOfFloat=sumOfFloat+number
number=number+0.1
print("Double floating point from 1 to 1000000 ",sumOfFloat)
number=1000000
##calculating the number by substracting the 0.5 every time and checking
while(number>=1):
sumOfFloatreverse=sumOfFloatreverse+number
number=number-0.1
print("Double floating point from 1000000 to 1",sumOfFloatreverse)
##check for equality
if(sumOfFloat==sumOfFloatreverse):
print("Both are same ")
else:
print("Both are not same")

CODE IMAGE:

sum.py - C:/Users/vinod/AppData/Local/Programs/Python/Python37-32/sum.py (3.7.2) File Edit Format Run Options Window Help ##s

OUTPUT:

sum.py - C:/Users/vinod/AppData/Local/Programs/Python/Python37-32/sum.py (3.7.2) File Edit Format Run Options Window Help ##s

Add a comment
Know the answer?
Add Answer to:
add 1 + 2 + 3 + 4 + .... + 1,000,000 = ? then add...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a calculator that will give the user this menu options: 1)Add 2)Subtract 3)Multiply 4)Divide 5)Exit...

    Write a calculator that will give the user this menu options: 1)Add 2)Subtract 3)Multiply 4)Divide 5)Exit . Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place. Please answer in python and make it simple. Please implement proper indentation, not just...

  • 1.Write a Python program to perform simple arithmetic operations (add, subtract, multiply and divide) on 2...

    1.Write a Python program to perform simple arithmetic operations (add, subtract, multiply and divide) on 2 numbers and display the output on console     ( use methods in your program) Range of numbers : 1 to 100 (Do not use 0)

  • Please write python code and please answer all parts and separate them into their parts, thank yo...

    Please write python code and please answer all parts and separate them into their parts, thank you.​​​​​​ Given the sequence: with the initial condition Xo chosen in the interval [0,1], and "a" is a given parameter between 0 and4 This sequence is a simple model to describe how a population (or civilization) evolves in a closed system with a finite amount of resources. The parameter "a" describes the rate of development and expansion of the civilization. The population is described...

  • Please in C Language Thank you! The following program source code is incomplete 1 #include <stdio.h> 2 3 // TODO:...

    Please in C Language Thank you! The following program source code is incomplete 1 #include <stdio.h> 2 3 // TODO: 1) Add the typedef here: 5// TODO: 2) Modify the parameter of repeat to add irn the function pointer for the function to be called repeatedly: 8 void repeat (int times) for (int k 0; k < times; ++k) 12 // TODO: 3) Add the call to the function pointer here: 14 15 17 void test (void) 18 printf("Test!\n"); 19...

  • 1 For a given processor bound workload the frequencies of instructions move (MOV), floating add (FADD),...

    1 For a given processor bound workload the frequencies of instructions move (MOV), floating add (FADD), and floating multiply (FMUL), and the corresponding instruction run times, for a given processor are: FADD 10 300 Others 50 160 MOV Instruction Frequency 30 Time [nanosec] 100 FMUL 10 600 What is the MIPS indicator of this processor? Faster memory chips reduce the MOV time by 50%, and all other times by 20%. What is now the value of the MIPS indicator? The...

  • 2. Consider the matrix c=110-3 10-3 10-3 o 10-3 (a) Apply the Gram-Schmidt process to the columns...

    linear algebra problem 2. Consider the matrix c=110-3 10-3 10-3 o 10-3 (a) Apply the Gram-Schmidt process to the columns of C, using the standard inner prod- uct. (b) Repeat part (a), this time using 3-digit floating point arithmetic. Is the result an (approximately) orthonormal set? 2. Consider the matrix c=110-3 10-3 10-3 o 10-3 (a) Apply the Gram-Schmidt process to the columns of C, using the standard inner prod- uct. (b) Repeat part (a), this time using 3-digit floating...

  • Define a std::vector<int> variable v and add 15 random integers into that container. Use the <random>...

    Define a std::vector<int> variable v and add 15 random integers into that container. Use the <random> header file and the “uniform_int_distribution” to generate values between 1 and 100, inclusive. <random> is available only in C++11 and newer. If you prefer, you can use floating-point values instead of ints, in that case, look up uniform_real_distribution -- if you decide to do that, use float or double instead of int in all subsequent parts of the lab. Write a print function that...

  • I need help with these 3 Assembly Language questions. If you could, please write/type legibly. 2,...

    I need help with these 3 Assembly Language questions. If you could, please write/type legibly. 2, Find the single precision floating point representation of each of the following numbers: (4 points) a) 175.5 b) -31.0 3, Find the double-precision floating point representation of each of the following numbers: (4 points) a) 175.5 b) -11.75 4, Find the decimal number comesponding to the following single-precision floating point representation C26A0000. points

  • 3. Write Python statements that will do the following: a) Input a string from the user....

    3. Write Python statements that will do the following: a) Input a string from the user. *** Print meaningful messages along with your output.* b) Print the length of the String. Example input: The sky is blue. Correct output: The length of the string is 16 Incorrect output: 16 c) Print the first character of the string. d) Print the last character of the string. 4. Save the program. Double-check the left edge for syntax errors or warning symbols before...

  • 1) Write a Python program that prompts the user to enter the current month name and...

    1) Write a Python program that prompts the user to enter the current month name and prints the season for that month. Hint: If the user enters March, the output should be "Spring"; if the user enters June, the output should be "Summer". 2 )Write a Python program using the recursive/loop structure to print out an equilateral triangle below (double spacing and one space between any two adjacent asterisks in the same row).          *      * * * *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT