Fortran Creating a Loop for Sum of Digit Squares to Equal 1

  • Thread starter Thread starter d-star
  • Start date Start date
  • Tags Tags
    Loop Squares Sum
AI Thread Summary
The discussion centers on creating a Fortran program that evaluates numbers from 1 to a user-defined limit (x) to determine if the sum of the squares of their digits eventually equals 1. The user has successfully implemented a function to calculate the sum of the squares but seeks guidance on structuring a loop to apply this function to each number in the specified range. The main challenge involves identifying whether each number's sequence reaches 1 (TRUE) or continues indefinitely (FALSE). The user aims to compile a list of numbers that meet the TRUE condition. Suggestions include formalizing the existing code into a callable function and utilizing it within a loop to check each number from 1 to x. The user has shared a partial code snippet and is looking for further recommendations on how to implement the logic for tracking and displaying results based on the criteria established.
d-star
Messages
8
Reaction score
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


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?
 


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
 


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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
1K
Replies
11
Views
2K
Replies
23
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
2
Views
2K
Back
Top