Creating a Loop for Sum of Digit Squares to Equal 1

In summary, you wrote a program to calculate the sum of the square of the individual digits of a number and eventually reach 1. However, you are having trouble finding a function to use to go through each number from 1 to a certain value x, and determine whether each number in between 1 and x meets this criteria or not.
  • #1
d-star
8
0
Hi, I'm writing a program in fortran that basically creates a loop from 1 until a certain number x (input by the user), and goes through each value between 1 and the certain number x in order to determine if each value meets certain criteria. The criteria is that the sum of the square of the individual digits created by each value in between 1 and x (where each value is taken from the preceding sum), must eventually equal 1. For example, the number 7 meets this criteria, because when we take the sum of the square of the individual digits, (7^2)=49, (4^2)+(9^2)=97, (9^2)+(7^2)=130, (1^2)+(3^2)+(0^2)=10, (1^2)+(0^2)= 1. The same goes for the number 13, except this reaches 1 after only two steps: (1^2)+(3^2)=10, (1^2)+(0^2)= 1.
I have already wrote the program for calculating the sum of the square of the individual digits of a number, and eventually reaching 1. Although I am having trouble finding a function to use in order to go through each number from 1 to a certain value x, and determining whether each number in between 1 and x meets this criteria or not.
Any help would be appreciated! Thanks!
 
Technology news on Phys.org
  • #2


d-star said:
Hi, I'm writing a program in fortran that basically creates a loop from 1 until a certain number x (input by the user), and goes through each value between 1 and the certain number x in order to determine if each value meets certain criteria. The criteria is that the sum of the square of the individual digits created by each value in between 1 and x (where each value is taken from the preceding sum), must eventually equal 1. For example, the number 7 meets this criteria, because when we take the sum of the square of the individual digits, (7^2)=49, (4^2)+(9^2)=97, (9^2)+(7^2)=130, (1^2)+(3^2)+(0^2)=10, (1^2)+(0^2)= 1. The same goes for the number 13, except this reaches 1 after only two steps: (1^2)+(3^2)=10, (1^2)+(0^2)= 1.
I have already wrote the program for calculating the sum of the square of the individual digits of a number, and eventually reaching 1. Although I am having trouble finding a function to use in order to go through each number from 1 to a certain value x, and determining whether each number in between 1 and x meets this criteria or not.
Any help would be appreciated! Thanks!

What have you tried?
 
  • #3


You are having trouble finding a function to use? You already wrote it! Just make a formal function out of your code and call it from within the loop of n = 1, x
 
  • #4


alright, thanks! it actually had a suspicion that that was the case. Although I have another question as well.
I also need to integrate a way that basically identifies if each value from 1 to x satisfies the condition that its sequence eventually reaches 1, and I also need to make my program identify if the sequence does not reach 1 (goes on forever). So if the number reaches 1 it is TRUE, and if does not reach 1 (goes on forever) then it is FALSE. I then need to be able to list all the numbers between 1 and x that are TRUE(sequence reaches 1). So far I have:

PROGRAM S
IMPLICIT NONE

INTEGER :: x,j,a
READ (*,*) x
j = 1

DO a = 1,x
CALL S
j = j +1
END DO

END PROGRAM S2


(where S is my function that solves for the square of the sum of the individual digits from 1 to x, determining if each number's sequence reaches 1 or not)

Any suggestions? Thanks!
 
  • #5


Hi there,

It sounds like you are on the right track with your program. In order to go through each number from 1 to x, you can use a for loop. This will allow you to iterate through each value and check if it meets the criteria you have set.

Here is an example of how you can use a for loop in your program:

do i = 1, x
! calculate the sum of the square of the individual digits for each value
! and store it in a variable called "sum"
sum = 0
num = i
do while (num > 0)
digit = mod(num, 10)
sum = sum + digit**2
num = num / 10
end do
! check if the sum eventually reaches 1
if (sum == 1) then
! if it does, print the number that meets the criteria
print*, i
end if
end do

This for loop will go through each value from 1 to x, calculate the sum of the square of the individual digits, and check if it eventually reaches 1. If it does, it will print the number that meets the criteria.

I hope this helps and good luck with your program! Let me know if you have any other questions.
 

1. What is the purpose of creating a loop for sum of digit squares to equal 1?

The purpose of creating a loop for sum of digit squares to equal 1 is to find the number of iterations it takes for a given number to reach the value of 1 when the sum of its digits squared is repeatedly calculated.

2. How does the loop for sum of digit squares work?

The loop for sum of digit squares works by taking a given number and repeatedly calculating the sum of its digits squared until it reaches the value of 1. If the sum of digits squared does not equal 1 after a certain number of iterations, the loop continues until the value of 1 is reached or a pattern is identified.

3. What is the significance of the sum of digit squares equaling 1?

The sum of digit squares equaling 1 is significant because it is known as the "happy number" in mathematics. This concept is related to number theory and has applications in various fields such as computer science and physics.

4. How can the loop for sum of digit squares be used in practical applications?

The loop for sum of digit squares can be used in practical applications such as data encryption and security systems. It can also be used in algorithms for solving mathematical problems and in testing the efficiency of software programs.

5. Are there any limitations or drawbacks to using a loop for sum of digit squares?

One limitation of using a loop for sum of digit squares is that it may take a large number of iterations to reach the value of 1 for certain numbers. This can make the process time consuming and computationally expensive. Additionally, the loop may not work for all types of numbers and may require further adjustments or modifications.

Similar threads

  • Programming and Computer Science
Replies
5
Views
837
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
735
  • Programming and Computer Science
Replies
4
Views
863
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
1
Views
937
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
30
Views
4K
Back
Top