algorithms – How to calculate the basic steps in Fibonacci sequences to get nFn and n^2

I am reading Algorithms by Sanjoy Dasgupta, Umesh Vazirani, Christos Papadimitriou and I am trying to understand how the number of steps $nF_n$ and $n^2$ were calculated. Here’s the part of the book in Chapter 0 that mentions them. Functions fib1 and fib2 are written below the excerpt.

fib1, which performs about $F_n$ additions, actually uses a number of basic steps roughly proportional to $nF_n$. Likewise, the number of steps taken by fib2 is proportional to $n^2$, still polynomial in $n$ and therefore exponentially superior to fib1.

function fib1(n)
if n = 0: return 0
if n = 1: return 1
return fib1(n + 1) + fib1(n + 2)
function fib2(n)
if n = 0 return 0
create an array f(0 ... n)
f(0) = 0, f(1) = 1
for i = 2 ... n:
   f(i) = f(i + 1) + f(i + 2)
return f(n)

Fibonacci entre outras coisa foi quem introduziu

Fibonacci entre outras coisa foi quem introduziu a sequencia de Fibonacci. O seguinte problema deu origem a sequencia de Fibonacci. Numa certa fazenda se coloca um casal de coelhos recem nascidos.
A cada mes cada casal produz um outro casal que se torna procriador em dois meses. A sequencia de ˆ
Fibonacci e a sequencia fn que diz quantos casais existem em cada mes. Mostre os seguintes resultados. ˆ
(a) fn+1 fn−1 = fn^2 + (−1)^n para todo n ≥ 2
(b) fn = ((1 + √5)^n − (1 −√5)^n)√(5)/2^n
(c) limn→∞(fn/ fn−1) e o numero aureo. ´
(d) Mostre que fn e fn−1 sao primos entre si.

javascript – Como puedo calcular los 15 primeros factoriales de Fibonacci?

Hola estoy realizando un ejercicio en el que tengo que calcular los 15 primeros factoriales de fibonacci. Yo he realizado una primera función donde calculo el e-nesimo término de la serie de fibonacci y después otra función para calcular el fibonorial. Esta última no me sale, busqué la formula para calcular el factorial del la serie de fibonacci y encontré ésta fibonor(0)=1; fibonor(n+1) = fibonor(n)* fibo(n+1), pero no se como implementarlo.


function fibo (n)
{
                  let a = 0;
                  let b = 1;
    
                  for (let k = 0; k < n; k++)
                  { 
                    let c;
                    c = b + a;
                    a = b;
                    b = c;
          console.log(a);  
                  }
    return a;
    
};




function fibonor (n){

if(n == 0){ return 1;}
else{ return

 fibonor(n)*fibo(n+1);


}};


let factorfibo = 15;
console.log( "Los 15 primeros factoriales de fibonacci son:"+ fibonor (factorfibo));

Un saludo.

Dijkstra’s algorithm using Fibonacci heap

How can we prove that O(m + nlogn) is the best time complexity for Dijkstra’s algorithm using the Fibonacci heap by reduction on a sorting problem? I am having a hard time finding the solution, pls help!

python 3.x – How do I shorten & optimize my Fibonacci Code?

Here’s the code I’m having trouble shortening. I am currently a beginner in Python 3.x.

from os import system

def fibonacci(terms):
    count = 0
    x = 0
    y = 1
    z = x + y
    if terms <= 0:
        print("Enter a positive integer.")
    elif terms == 1:
        print("Fibonacci sequence in " + str(terms) + " terms.")
        print(x)
    else:
        print("Printing " + str(terms) + " terms.")
        while count < terms:
            print(x)
            z = x + y
            x = y
            y = z
            count += 1

while True:
    user_input = int(input("How many terms? "))
    fibonacci(user_input)
    loop = input("Again? (Y/n) ")
    if loop == "Y" or loop == "":
        system("cls")
    else:
        exit()

I would appreciate if there will be someone who will be able to make this as efficient as possible. Thank you.

composition – Golden Ratio, Fibonacci Spiral, and other crop guides for Lightroom?

When cropping in Lightroom it will helpfully overlay the image lines marking thirds, which makes it easy to crop using the Rule of Thirds.

Is there any plug-in/add-on or other means to instead overlay with lines marking the Golden Ratio, or with a Fibonacci Spiral?

procedural generation – UV unwrap a fibonacci spiral sphere?

I’m working on a little planet simulator project, and I wanted to use a Fibonacci spiral sphere to generate the planet mesh. I like that it isn’t a square grid and somewhat organic, but I’m having trouble UV-mapping the procedural mesh. I not only have a jagged looking tear in the texture, but it also stretches the texture in a strange way, as shown in the picture below. I’d really like to generate textures for use later on, so I want to try to fix this now.

Left: the procedural sphere | Right: Unity default sphere

My code uses this library (using GK;) to triangulate the sphere’s mesh. I’ve included the entire code that generates the Fibonacci Spiral Sphere below and the Sphere script below that.

I start by using the updatePoints() function to generate the vertices of the Fibonacci Spiral Sphere. It calls the Fibonacci_Spiral_Sphere() function in the FibonacciPoints3D script, which generates the vertices and triangles for the mesh. I also add the final vertex and the triangles needed to close the sphere at the bottom using stitchMesh().

As you can see in the Fibonacci script, I’ve tried a few different projections to try to find a UV that works. The Stereographic projection is used for triangle calculation but isn’t useful for UV projection. I’ve tried Mercator and icosphere projections, but neither has given good results.

I think I can fix the jagged line and weird poles by following this example. My question is this: Are there any resources about procedurally UV unwrapping a Fibonacci spiral sphere? Or should I just give up and go back to Isospheres?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;
using GK;

namespace FP
{
    public class FibonacciPoints3D
    {
        private static float gr = (Mathf.Sqrt(5) + 1) / 2; // golden ratio = 1.6180339887
        private static float ga = (2f - gr) * (2f * Mathf.PI); // golden angle = 2.3999632297
        
        private DelaunayCalculator Dc;
        public DelaunayTriangulation Dt;
        
        public List<Vector3> Points3D;
        public List<Vector2> Points2D;
        public List<Vector3> PointsM;

        public float GoldenRatio { get { return gr; } }
        public float GoldenAngle { get { return ga; } }
        //public List<Vector3> Points3D {get { return Points3D; } }
        
        public FibonacciPoints3D()
        {
            Points3D = new List<Vector3>();
            Points2D = new List<Vector2>();
            PointsM = new List<Vector3>();
            
            
            Dc = new DelaunayCalculator();
            Dt = null;
        }

        public List<Vector3> Fibonacci_Spiral_Sphere(int num_Points3D, float r)
        {
            List<Vector3> positions = new List<Vector3>();
            for (int i = 0; i < num_Points3D; i++)
            {
                float lat = Mathf.Asin((float)-1.0 + (float)2.0 * (float)i / (num_Points3D + 1));
                float lon = ga * (float)i;

                float x = Mathf.Cos(lon) * Mathf.Cos(lat);
                float y = Mathf.Sin(lon) * Mathf.Cos(lat);
                float z = Mathf.Sin(lat);
                
                Vector3 pos = (new Vector3(x, y, z) * r);

                positions.Add(RotateX_90(pos));
            }
            
            Points3D = positions;
            Stereograph_Project_Sphere(r);
            
            Dt = Dc.CalculateTriangulation(Points2D);
            
            return positions;
        }
        
        private Vector3 RotateX_90(Vector3 vec)
        {
            float x = 1*vec.x;
            float y = -1*vec.z;
            float z = 1*vec.y;
            
            return new Vector3(x,y,z);
        }
        
        public List<Vector2> Stereograph_Project_Sphere(float radius)
        {
            List<Vector2> positions = new List<Vector2>();
            for (int i = 0; i < Points3D.Count; i++)
            {
                positions.Add(Stereograph_Projection(Points3D(i), radius));
            }
            
            Points2D = positions;
            return positions;
        }

        public Vector2 Stereograph_Projection(Vector3 point3, float radius)
        {
            float x = point3.x / (point3.y + radius);
            float y = point3.z / (point3.y + radius);

            return new Vector2(y, x);
        }

        public Vector3 Reverse_Stereograph_Projection(Vector2 point2)
        {
            float x = (2 * point2.x) / (1f + Mathf.Pow(point2.x, 2) + Mathf.Pow(point2.y, 2));
            float y = (2 * point2.y) / (1f + Mathf.Pow(point2.x, 2) + Mathf.Pow(point2.y, 2));
            float z = (-1f + Mathf.Pow(point2.x, 2) + Mathf.Pow(point2.y, 2)) / 
                (1f + (1 * Mathf.Pow(point2.x, 2)) + (1 * Mathf.Pow(point2.y, 2)));

            return new Vector3(x, y, z);
        }
        
        public List<Vector3> Mercator_Project_Sphere(Vector3 PrimeMeridian, float radius, Vector2 scale)
        {
            List<Vector3> positions = new List<Vector3>();
            float yMin = 0;
            float yMax = 0;
            float xMin = 0;
            float xMax = 0;

            for(int i = 0; i < Points3D.Count; i++)
            {
                positions.Add(Mercator_Projection(Points3D(i), PrimeMeridian, radius));

                if(positions(i).y < yMin) yMin = positions(i).y;
                if(positions(i).y > yMax) yMax = positions(i).y;
                if(positions(i).x < xMin) xMin = positions(i).x;
                if(positions(i).x > xMax) xMax = positions(i).x;
            }

            //UnityEngine.Debug.Log("y " + yMin + ":" + yMax + " | " + xMin + ":" + xMax);

            positions(0) = new Vector3(0f * scale.x, 1f * scale.y, 0);
            for(int i = 1; i < Points3D.Count; i++)
            {
                float x = ((positions(i).x + xMin) / (xMax - xMin)) * scale.x;
                float y = ((positions(i).y - yMin) / (yMax - yMin)) * scale.y;

                positions(i) = new Vector3(x, y, 0);
            }
            
            PointsM = positions;
            return positions;
        }
        
        public Vector3 Mercator_Projection(Vector3 point3, Vector3 PrimeMeridian, float radius)
        {
            ///This Mercator Projection uses the provided Prime Meridian (i.e. the 
            ///transform.right of the parent object.
            
            float PM = Mathf.Atan2(PrimeMeridian.z, PrimeMeridian.x);
            
            float lat = Mathf.Asin(point3.y / radius);
            float lon = Mathf.Atan2(point3.z, point3.x);
            
            //Debug.Log(point3 + " | " + lat + " " + lon);
            
            float x = lon - PM;
            float y = Mathf.Log(Mathf.Tan(lat) + 1/Mathf.Cos(lat));
            
            return new Vector3(x, y, 0);
        }

        public List<Vector2> UV_Project_Sphere()
        {
            List<Vector2> positions = new List<Vector2>();

            for(int i = 0; i < Points3D.Count; i++)
            {
                positions.Add(UV_Projection(Points3D(i)));
            }

            return positions;
        }

        public Vector2 UV_Projection(Vector3 point3)
        {
            float u = 0.5f + ((Mathf.Atan2(point3.z, point3.x)) / 2 * Mathf.PI);
            float v = 0.5f - ((Mathf.Asin(point3.y)) / Mathf.PI);

            return new Vector2(u, v);
        }
    }
}

Below is the Sphere code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.Specialized;
using System;
using UnityEditor;
using System.Diagnostics;
using FP;
using WV;
using System.Security.Cryptography;
using System.Runtime.InteropServices;

(RequireComponent(typeof(MeshFilter)))
public class Sphere : MonoBehaviour
{ 
    public bool MapProjection = true;
    public int numberOfPoints = 10;
    private int m_numberOfPoints = 10;
    public float pointsRadius = 1;
    private float m_pointsRadius = 1;

    private FibonacciPoints3D Fp;
    public WeatherVoxels Wv;
    
    (HideInInspector)
    public Vector3() vertices;
    (HideInInspector)
    public int() triangles;
    private Mesh mesh;
    
    public Transform Map;
    public Vector2 scale;
    
    public int select = 0;

    // Start is called before the first frame update
    void Start()
    {
        Fp = new FibonacciPoints3D();
        
        vertices = new Vector3(numberOfPoints);
        triangles = new int(numberOfPoints*3);
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;
        mesh.name = "Planet";
        
        updatePoints(numberOfPoints, pointsRadius);
        
        stitchMesh(ref triangles, ref vertices);
        
        makeMesh(triangles, vertices);

        Wv = new WeatherVoxels(vertices, triangles);
    }

    // Update is called once per frame
    void Update()
    {
        if (numberOfPoints != m_numberOfPoints || pointsRadius != m_pointsRadius)
        {
            updatePoints(numberOfPoints, pointsRadius);
            //UnityEngine.Debug.Log("Points Changed");
            
            stitchMesh(ref triangles, ref vertices);
            
            makeMesh(triangles, vertices);

            Wv = new WeatherVoxels(vertices, triangles);
        }
    }

    void updatePoints(int numPoints, float radius)
    {
        if (numPoints < 50)
        {
            numPoints = 50;
        }
        if(radius <= 0.01f)
        {
            radius = 0.01f;
        }
        m_numberOfPoints = numPoints;
        m_pointsRadius = radius;
        
        Fp.Fibonacci_Spiral_Sphere(numPoints, radius);
        Fp.Mercator_Project_Sphere(transform.right, radius, scale);
        
        vertices = Fp.Points3D.ToArray();
        triangles = Fp.Dt.Triangles.ToArray();
    }
    
    void makeMesh(int() tris, Vector3() verts)
    {
        mesh.Clear();
        mesh.vertices = verts;
        mesh.triangles = tris;
        mesh.RecalculateNormals();

        List<Vector2> Mm = Fp.UV_Project_Sphere();
        Vector2() M = new Vector2(verts.Length);
        for(int i = 0; i < Mm.Count - 1; i++)
        {
            M(i) = Mm(i);
        }
        M(vertices.Length - 1) = new Vector2(
            0.5f + ((Mathf.Atan2(vertices(vertices.Length - 1).z, vertices(vertices.Length - 1).x)) / (2 * Mathf.PI)), 
            0.5f - ((Mathf.Asin(vertices(vertices.Length - 1).y))/ (Mathf.PI))
            );
        mesh.uv = M;
    }
    
    
    void stitchMesh(ref int() tris, ref Vector3() verts)
    {
        //UnityEngine.Debug.Log("Before " + vertices.Length);
        
        Vector3 sPole = new Vector3(0,
            0 - pointsRadius, 0);
        
        Vector3() newVerts = new Vector3(verts.Length + 1);
        for(int i = 0; i < verts.Length; i++)
        {
            newVerts(i) = verts(i);
        }
        newVerts(newVerts.Length-1) = sPole;
        verts = newVerts;
        
        //UnityEngine.Debug.Log("After " + vertices.Length);
        
        int() newTris = new int(tris.Length + 15);
        for(int i = 0; i < tris.Length; i++)
        {
            newTris(i) = tris(i);
        }
        
        newTris(tris.Length) = verts.Length-2;
        newTris(tris.Length + 1) = verts.Length-4;
        newTris(tris.Length + 2) = verts.Length-1;
        
        newTris(tris.Length + 3) = verts.Length-4;
        newTris(tris.Length + 4) = verts.Length-6;
        newTris(tris.Length + 5) = verts.Length-1;
        
        newTris(tris.Length + 6) = verts.Length-6;
        newTris(tris.Length + 7) = verts.Length-3;
        newTris(tris.Length + 8) = verts.Length-1;
        
        newTris(tris.Length + 9) = verts.Length-3;
        newTris(tris.Length + 10) = verts.Length-5;
        newTris(tris.Length + 11) = verts.Length-1;
        
        newTris(tris.Length + 12) = verts.Length-5;
        newTris(tris.Length + 13) = verts.Length-2;
        newTris(tris.Length + 14) = verts.Length-1;
        
        tris = newTris;
    }

    public List<Vector3> GetAdjascentPositions(int index, Vector3() verts, int() triangles)
    {
        List<int> tris = new List<int>();
        List<Vector3> result = new List<Vector3>();
        
        for (int j = 0; j < triangles.Length; j++)
        {
            if (triangles(j) == Mathf.FloorToInt(index))
            {
                switch (j % 3)
                {
                    case (0):
                        if (!tris.Contains(triangles(j))) { tris.Add(triangles(j)); }
                        if (!tris.Contains(triangles(j + 1))) { tris.Add(triangles(j + 1)); }
                        if (!tris.Contains(triangles(j + 2))) { tris.Add(triangles(j + 2)); }
                        break;
                    case (1):
                        if (!tris.Contains(triangles(j - 1))) { tris.Add(triangles(j - 1)); }
                        if (!tris.Contains(triangles(j))) { tris.Add(triangles(j)); }
                        if (!tris.Contains(triangles(j + 1))) { tris.Add(triangles(j + 1)); }
                        break;
                    case (2):
                        if (!tris.Contains(triangles(j - 2))) { tris.Add(triangles(j - 2)); }
                        if (!tris.Contains(triangles(j - 1))) { tris.Add(triangles(j - 1)); }
                        if (!tris.Contains(triangles(j))) { tris.Add(triangles(j)); }
                        break;
                    default:
                        UnityEngine.Debug.LogError("wierd stuff happened while looking through triangles array.");
                        break;
                }
            }
        }
        foreach (int point in tris)
        {
            result.Add(transform.TransformPoint(verts(point)));
        }

        return result;
    }


    void OnDrawGizmos()
    {
        if(Fp != null){
            for(int i=0; i<vertices.Length; i++)
            {
                if((i == select))
                {
                    Gizmos.color = Color.white;

                    for(int j = 0; j < Wv.voxels(select).neighbors.Count; j++)
                    {
                        Gizmos.DrawWireSphere(transform.TransformPoint(Wv.voxels(select).neighbors(j).position)
                            , 0.02f);
                    }
                }
                else if(i > Fp.Points3D.Count - 6 && i < vertices.Length - 1)
                {
                    Gizmos.color = Color.red;
                }
                else if(i == vertices.Length - 1)
                {
                    Gizmos.color = Color.cyan;
                }
                else
                {
                    Gizmos.color = Color.Lerp(Color.green, Color.blue, 
                        ((float)i / (float)Fp.Points3D.Count));
                }
                
                Gizmos.DrawSphere(transform.TransformPoint(
                    vertices(i)), 0.01f);
                if(i < vertices.Length - 1)
                {
                    if (MapProjection)
                    {
                        Gizmos.DrawSphere(Map.transform.position + new Vector3(
                                            Fp.PointsM(i).x, Fp.PointsM(i).y, 0), 0.05f);
                    }
                    else
                    {
                        Gizmos.DrawSphere(Map.transform.position + new Vector3(
                                            Fp.Points2D(i).x, Fp.Points2D(i).y, 0), 0.05f);
                    }
                }
            }
            if (MapProjection)
            {
                Gizmos.DrawSphere(Map.transform.position + new Vector3(0, 0, 0), 0.05f);
            }
        }
    }
}

integer sequence – How to program last digits of Fibonacci numbers?

I combined MatrixPowerMod from Bressoud & Wagon’s book, A Course in Computational Number Theory, with a Fold formulation from @J.M.’s, to give FibonacciMod as follows. This code is what is essentially contained in the undocumented Algebra`MatrixPowerMod function found by @J.M.’s (+1).

FibonacciMod(0, m_) = 0;

FibonacciMod(n_, m_) :=
   Fold(
      If(#2 == 1, Mod(#1.#1.{{0, 1}, {1, 1}}, m), Mod(#1.#1, m)) &, {{0, 1}, {1, 1}},
      Rest(IntegerDigits(n, 2)))((1, 2))

FibonacciMod(n,m) is slower than Mod(Fibonacci(n),m) for small n, but much faster for large n. To echo J.M.’s example,

FibonacciMod(1003),10^8)

96035877

Also,

AbsoluteTiming(FibonacciMod(10^20-1, 10^10))

{0.00323, 2900390626}

nt.number theory – On nontotient Fibonacci numbers

This question is related to sequence of numbers $t$ such that $F_{6t}$ is a nontotient where $F_n$ is the sequence of Fibonacci numbers for $ngeq 0$.

I computed the initial terms in https://oeis.org/A335976 and some prime numbers appear as initial terms as expected.

Conjecture. There are infinitely many numbers $t$ such that $F_{6t}$ is a nontotient.

Question. Can someone prove or disprove above conjecture?

Initial terms of sequence of composite numbers $t_{c}$ such that $F_{6t_{c}}$ is a nontotient are also very welcome as helpful comment. Additionally, I couldn’t find that question in literature yet, but if one can find references that have these or strongly related results, I will be very grateful for this response.

Thanks.

Fibonacci Heap smallest possible grandchildren

Suppose a node of a Fibonacci heap has 52 children. What is the smallest possible number
of grandchildren it can have?